vue-router query,parmas,meta传参

28 篇文章 1 订阅

1.query,显示在导航栏?后,相当于get请求传参

this.router.push({path:'/login',query:{ 'redirect':'/home'}})
this.router.push({name:'Login',query:{ 'redirect':'/home'}})

2.parmas,不会显示,相当于post请求传参, 目前测试没有query不行,而且刷新后消失

this.router.push({name:'Login',parmas:{ 'redirect':'/home'}})
this.router.push({name:'Login',query:{ 'redirect':'/home'}},parmas:{ 'redirect':'/home'}})

在vue中使用parmas传参时必须用name跳转,而且必须和query一起传?否则接收不到;或者路由中有设置/:XX来接收这个参数

3.meta,不显示,测试失败

this.router.push({name:'Login',meta:{ 'redirect':'/home'}}) 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 Vue.js 进行前端开发时,经常需要进行页面的跳转和参数的传递。Vue.js 提供了一个官方的路由插件 vue-router,可以方便地进行页面的跳转和参数的传递。 vue-router 支持两种方式进行参数传递:params 和 query。 params 传参: params 是路由路径中的占位符,可以通过 $route.params 来获取传递的参数。使用 params 传参时,需要在定义路由规则时使用冒号将参数名包裹起来,例如: ```javascript const router = new VueRouter({ routes: [ { path: '/user/:id', component: User } ] }) ``` 在上面的例子中,定义了一个 /user/:id 的路由规则,其中 :id 是一个占位符,可以接受任意的参数。 在组件中访问传递的参数时,可以通过 $route.params.id 来获取: ```javascript const User = { template: '<div>User {{ $route.params.id }}</div>' } ``` 在使用 params 传参时需要注意,如果在同一个路由规则下,参数发生变化时不会触发组件的生命周期钩子函数,需要通过 watch 监听 $route 对象来实现: ```javascript const User = { template: '<div>User {{ id }}</div>', watch: { '$route.params.id' (to, from) { this.id = to } }, data () { return { id: '' } } } ``` query 传参query 是通过 URL 的查询字符串传递参数,可以通过 $route.query 来获取传递的参数。使用 query 传参时,需要在定义路由规则时使用问号将参数名包裹起来,例如: ```javascript const router = new VueRouter({ routes: [ { path: '/user', component: User } ] }) ``` 在组件中访问传递的参数时,可以通过 $route.query.id 来获取: ```javascript const User = { template: '<div>User {{ $route.query.id }}</div>' } ``` 在使用 query 传参时需要注意,参数的值会被转换为字符串类型,如果需要传递数组或对象等复杂数据类型,需要将其转换为 JSON 字符串。在组件中获取参数时需要使用 JSON.parse 解析字符串。例如: ```javascript // 在路由中传递数组参数 router.push({ path: '/user', query: { ids: JSON.stringify([1, 2, 3]) } }) // 在组件中获取数组参数 const User = { template: '<div>User {{ ids }}</div>', computed: { ids () { return JSON.parse(this.$route.query.ids) } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值