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

来源:互联网 发布:风暴大陆坐骑进阶数据 编辑:程序博客网 时间:2024/06/05 17:24

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

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

2. $router方式跳转

// 组件 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>




原创粉丝点击