路由传参的三种方式

由带参数:

传参方式可划分为 params 传参和 query 传参,而 params 传参又可分为在 url 中显示参数和不显示参数两种方式

1.params 传参(显示参数)又可分为 声明式 和 编程式 两种方式

  • 声明式router-link:该方式是通过router-link组件的to属性实现,子路由需要提前配置好参数

<router-link :to="/child/1"> 跳转到子路由 </router-link>

{
    path: '/child/:id',
    component: Child
}
  • 编程式 this.$router.push:同样需要子路由提前配置好参数。
    {
        path: '/child/:id',
        component: Child
    }
      
       this.$router.push({
         path:'/child/${id}',
       })

    接收: this.$route.params.id

  • 2. params传参(不显示参数)也可分为声明式和编程式两种方式,与显示参数不同的是,这里是通过路由的别名 name 进行传值的

    <router-link :to="{name:'Child',params:{id:1}}">跳转到子路由</router-link>
    
    {
        path: '/child,
        name: 'Child',
        component: Child
    }
      
       this.$router.push({
         name:'Child',
         params:{
          id:1
         }
       })

    接收: this.$route.params.id

  • 3.query 传参(显示参数)也可分为声明式和编程式 两种方式

  • 声明式router-link:该方式是通过 router-link 组件的 to 属性实现,不过使用该方式传值的时候,需要子路由提前配置好路由别名
    {
        path: '/child,
        name: 'Child',
        component: Child
       }
       
       <router-link :to="{name:'Child',query:{id:1}}">跳转到子路由</router-link>

  • 编程式 this.$router.push:使用该方式传值的时候,同样需要子路由提前配置好路由别名(name 属性)
    {
        path: '/child,
        name: 'Child',
        component: Child
       }
      
       this.$router.push({
         name:'Child',
         query:{
          id:1
         }
       })

    接收: this.$route.query.id

  • 14
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值