VUE 路由父子传参的方式

方案一:
getDescribe(id) {
// 直接调用 r o u t e r . p u s h 实 现 携 带 参 数 的 跳 转 t h i s . router.push 实现携带参数的跳转 this. router.pushthis.router.push({
path: /describe/${id}, //路由地址
})

方案一,需要对应路由配置如下:
{
path: ‘/describe/:id’,
name: ‘组件名’,
component: 组件名
}
需要在path中添加/:id来对应 $router.push 中path携带的参数。在子组件中可以使用来获取传递的参数值。

子组件中: 这样来获取参数
this.$route.params.id

方案二:

this.$router.push({
      name:'组件名',
      params:{
      		id:id
      }
    })

方案二,需要对应路由配置如下: 注意这里不能使用:/id来传递参数了,因为父组件中,已经使用params来携带参数了。

 {
     path: '/describe',
     name: '组件名',
     component: 组件名
   }      
子组件中: 这样来获取参数
this.$route.params.id   
Logo

前往低代码交流专区

更多推荐