首页  >  文章  >  web前端  >  vuejs怎么实现路由跳转传值

vuejs怎么实现路由跳转传值

青灯夜游
青灯夜游原创
2021-10-26 14:51:442376浏览

vuejs路由跳转传值的方法:1、使用“3b88b06f64aae7672ef29d18cd6ed8e2”语句进行跳转传值;2、使用“this.$router.push({ path:’/user’})”语句进行跳转传值。

vuejs怎么实现路由跳转传值

本教程操作环境:windows7系统、vue2.9.6版,DELL G3电脑。

vue中路由跳转传参数有多种,自己常用的是下面的几种

  • 通过router-link进行跳转

  • 通过编程导航进行路由跳转

 1、router-link

<router-link 
 :to="{
  path: &#39;yourPath&#39;, 
  params: { 
   name: &#39;name&#39;, 
   dataObj: data
  },
  query: {
   name: &#39;name&#39;, 
   dataObj: data
  }
 }">
</router-link>

 1. path -> 是要跳转的路由路径,也可以是路由文件里面配置的 name 值,两者都可以进行路由导航
 2. params -> 是要传送的参数,参数可以直接key:value形式传递
 3. query -> 是通过 url 来传递参数的同样是key:value形式传递

 // 2,3两点皆可传递

2、$router方式跳转

// 组件 a
<template>
 <button @click="sendParams">传递</button>
</template>
<script>
 export default {
 name: &#39;&#39;,
 data () {
  return {
  msg: &#39;test message&#39;
  }
 },
 methods: {
  sendParams () {
  this.$router.push({
   path: &#39;yourPath&#39;, 
   name: &#39;要跳转的路径的 name,在 router 文件夹下的 index.js 文件内找&#39;,
   params: { 
    name: &#39;name&#39;, 
    dataObj: this.msg
   }
   /*query: {
    name: &#39;name&#39;, 
    dataObj: this.msg
   }*/
  })
  }
 },
 computed: {

 },
 mounted () {

 }
 }
</script>
<style scoped></style>
----------------------------------------
// 组件b
<template>
 <h3>msg</h3>
</template>
<script>
 export default {
 name: &#39;&#39;,
 data () {
  return {
  msg: &#39;&#39;
  }
 },
 methods: {
  getParams () {
  // 取到路由带过来的参数 
  let routerParams = this.$route.params.dataobj
  // 将数据放在当前组件的数据内
  this.msg = routerParams
  }
 },
 watch: {
 // 监测路由变化,只要变化了就调用获取路由参数方法将数据存储本组件即可
  &#39;$route&#39;: &#39;getParams&#39;
 }
 }
</script>
<style scoped></style>

相关推荐:《vue.js教程

以上是vuejs怎么实现路由跳转传值的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn