vue2.0 点击跳转传参--vue路由跳转传参数 - CSDN博客

205 阅读3分钟
原文链接: blog.csdn.net

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

  • 通过router-link进行跳转
  • 通过编程导航进行路由跳转
1. router-link
  1. <router-link   
  2.     :to="{  
  3.         path: 'yourPath',   
  4.         params: {   
  5.             name: 'name',   
  6.             dataObj: data  
  7.         },  
  8.         query: {  
  9.             name: 'name',   
  10.             dataObj: data  
  11.         }  
  12.     }">  
  13. </router-link>  
  14.   
  15.  1. path -> 是要跳转的路由路径,也可以是路由文件里面配置的 name 值,两者都可以进行路由导航  
  16.  2. params -> 是要传送的参数,参数可以直接key:value形式传递  
  17.  3. query -> 是通过 url 来传递参数的同样是key:value形式传递  
  18.   
  19.  // 2,3两点皆可传递  
<router-link 
    :to="{
        path: 'yourPath', 
        params: { 
            name: 'name', 
            dataObj: data
        },
        query: {
            name: 'name', 
            dataObj: data
        }
    }">
</router-link>

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

 // 2,3两点皆可传递
2. $router方式跳转
  1. // 组件 a  
  2. <template>  
  3.     <button @click="sendParams" >传递</button>  
  4. </template>  
  5. <script>  
  6.   export default {  
  7.     name: '',  
  8.     data () {  
  9.       return {  
  10.         msg: 'test message'  
  11.       }  
  12.     },  
  13.     methods: {  
  14.       sendParams () {  
  15.         this.$router.push({  
  16.             path: 'yourPath',   
  17.             name: '要跳转的路径的 name,在 router 文件夹下的 index.js 文件内找',  
  18.             params: {   
  19.                 name: 'name',   
  20.                 dataObj: this.msg  
  21.             }  
  22.             /*query: {  
  23.                 name: 'name',   
  24.                 dataObj: this.msg  
  25.             }*/  
  26.         })  
  27.       }  
  28.     },  
  29.     computed: {  
  30.   
  31.     },  
  32.     mounted () {  
  33.   
  34.     }  
  35.   }  
  36. </script>  
  37. <style scoped></style>  
  38. ----------------------------------------  
  39. // 组件b  
  40. <template>  
  41.     <h3>msg</h3>  
  42. </template>  
  43. <script>  
  44.   export default {  
  45.     name: '',  
  46.     data () {  
  47.       return {  
  48.         msg: ''  
  49.       }  
  50.     },  
  51.     methods: {  
  52.       getParams () {  
  53.         // 取到路由带过来的参数   
  54.         let routerParams = this.$route.params.dataobj  
  55.         // 将数据放在当前组件的数据内  
  56.         this.msg = routerParams  
  57.       }  
  58.     },  
  59.     watch: {  
  60.     // 监测路由变化,只要变化了就调用获取路由参数方法将数据存储本组件即可  
  61.       '$route': 'getParams'  
  62.     }  
  63.   }  
  64. </script>  
  65. <style scoped></style>  
// 组件 a
<template>
    <button @click="sendParams">传递</button>
</template>
<script>
  export default {
    name: '',
    data () {
      return {
        msg: 'test message'
      }
    },
    methods: {
      sendParams () {
        this.$router.push({
            path: 'yourPath', 
            name: '要跳转的路径的 name,在 router 文件夹下的 index.js 文件内找',
            params: { 
                name: 'name', 
                dataObj: this.msg
            }
            /*query: {
                name: 'name', 
                dataObj: this.msg
            }*/
        })
      }
    },
    computed: {

    },
    mounted () {

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