杰网资源 Design By www.escxy.com

日常业务中,路由跳转的同时传递参数是比较常见的,传参的方式有三种:

1)通过动态路由方式

//路由配置文件中 配置动态路由
{
   path: '/detail/:id',
   name: 'Detail',
   component: Detail
  }
//跳转时页面
var id = 1;
this.$router.push('/detail/' + id)
 
//跳转后页面获取参数
this.$route.params.id

2)通过query属性传值

//路由配置文件中
{
   path: '/detail',
   name: 'Detail',
   component: Detail
  }
//跳转时页面
this.$router.push({
 path: '/detail',
 query: {
  name: '张三',
  id: 1,
 }
})
 
//跳转后页面获取参数对象
this.$route.query 

3)通过params属性传值

//路由配置文件中
{
   path: '/detail',
   name: 'Detail',
   component: Detail
  }
//跳转时页面
this.$router.push({
 name: 'Detail',
 params: {
  name: '张三',
  id: 1,
 }
})
 
//跳转后页面获取参数对象
this.$route.params 

总结:

1.动态路由和query属性传值 页面刷新参数不会丢失, params会丢失

2.动态路由一般用来传一个参数时居多(如详情页的id), query、params可以传递一个也可以传递多个参数 。

补充方法:

页面刷新数据不会丢失

methods:{
 insurance(id) {
    //直接调用$router.push 实现携带参数的跳转
    this.$router.push({
     path: `/particulars/${id}`,
    })
}

需要对应路由配置如下:

this.$route.params.id
标签:
vue,路由跳转

杰网资源 Design By www.escxy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
杰网资源 Design By www.escxy.com