路由传参并接收参数

2019-02-20  本文已影响0人  BugMyGod
//传参: 
this.$router.push({
        path:'/xxx'
        query:{
          id:id
        }
      })
//接收参数:
this.$route.query.id

ps:传参时是this.$router,接收参数是this.$route
1.$router为VueRouter实例,想要导航到不同URL,则使用$router.push方法
2.$route为当前router跳转对象,里面可以获取name、path、query、params


//传参: 
this.$router.push({
        name:'xxx'
        params:{
          id:id
        }
      })
//接收参数:
this.$route.params.id

ps:params传参,push里面只能是 name:'xxxx',不能是path:'/xxx',因为params只能用name来引入路由,如果这里写成了path,接收参数页面会是undefined!!!

区别:query相当于get请求,页面跳转的时候,可以在地址栏看到请求参数,而params相当于post请求,参数不会再地址栏中显示

上一篇 下一篇

猜你喜欢

热点阅读