【Vue】路由传参方式

在使用params传递参数时,需要使用name来指定目标,否则会出现数据传输失败

vue的路由传参共有三种方式

1.query传参:不管使用path还是name来匹配路由都可以,然后通过query来传递参数,这种情况下 query传递的参数会显示在url(浏览器上看到的链接)后面

//使用标签
<router-link :to='{path:"test_path",query:{id:1}}'>传1</router-link>
<router-link :to='{path:"/test_path",query:{id:2}}'>传2</router-link>
<router-link :to='{name:"test_path",query:{id:3}}'>传3</router-link>

//js
<button @click="btn1()">传1</button>
<button @click="btn2()">传2</button>

btn1(){
      this.$router.push({path:'about',query:{id:1}})
},
btn2(){
      this.$router.push({name:'about',query:{id:2}})
},

2.params传参:通过路由属性中的name来确定匹配的路由,通过params来传递参数。

<!-- 传递params参数时,是通过指定路由名称进行传递的 -->
<router-link :to='{ name: "test_path", params: { id: 3 } }'>传3</router-link>

//js
<button @click="btn2()">传2</button>
btn2(){
      this.$router.push({name:'test_path',params:{id:2}})
},

3.路径传参:直接通过path,直接携带参数进行路由传参,使用该方式传值的时候,需要子路由提前配置好参数,接收时也是通过this.$route.params.id获取

配置如下:

{
  path: '/test_path/:id',
  name: 'test_path',
  component: test_path
}
<router-link to='/test_path/1'>传1</router-link>

//js
<button @click="btn1()">传1</button>
btn1(){
      this.$router.push({path:'/about/1'})
    },

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shino_jie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值