VUE - 路由传参的三种方式

1.  在路由中配置 path

{
    path:'product/:id',
    name:"product",
    component:Product,
},

 传递参数,页面刷新数据不会消失

  getProductInfo(){
     // 获取路由参数
     let id = this.$route.params.id;
     this.axios.get(`/products/${id}`).then(res=>{
        this.product = res
     })
  },    

获取参数

this.$route.params.id

 

2.  query

使用 path 来匹配路由

 this.$router.push({
     path:'/index',
     query:{from:"login"}
 })

地址栏会显示相应参数

获取参数

this.$route.query.from

 

 

 

 3. params

使用 name 来匹配路由

this.$router.push({
     name:'index',
     params:{from:"login"}
})

地址栏不会显示参数

获取参数

this.$route.params.from

 

posted @ 2020-03-10 21:11  .xiao  阅读(754)  评论(0编辑  收藏  举报