在做VUE项目时遇到的问题及解决办法:使用路由跳转页面时传参的两种方法,以及这两种方法的综合使用

一、使用path和query,使用该方式跳转的页面在路径中会携带query中的参数,这种方式重新加载页面后参数依然存在;

1.通过router-link进行跳转:

<router-link to="{path:'/search-list-all',query:{searchVal:searchInfo}}"></router-link>

2.使用$router方式跳转,此方法写在methods里:

this.$router.push({
  path:'/search-list-all',
  query:{
    searchVal:searchInfo
  }
})

注意:在这里path是路由的路径

在跳转后的页面接收参数(获取时是$route而不是$router)

this.$route.query.searchVal

二、使用name和params,使用params传参会导致页面刷新时,记录不了参数;

1.通过router-link进行跳转:

<router-link to="{name:'SearchListAll',params:{searchVal:searchInfo}}"></router-link>

2.使用$router方式跳转,此方法写在methods里:

this.$router.push({
  name:'SearchListAll',
  params:{
    searchVal:searchInfo
  }
})

注意:在这里name是路由的名称name

在跳转后的页面接收参数(获取时是$route而不是$router)

this.$route.params.searchVal

在路由里定义的路径如下:

export default new Router({
    routes: [
        {
          path: '/search-list-all',
          name: 'SearchListAll',
          component: SearchListAll
        }
    ]
})

最后,是这两种方式的综合使用(即在页面跳转时,使用query和params传参):

    this.$router.push({
        path:'/search-list-all',
        name: 'SearchListAll',
        query:{
          secondId:secondId,
          thirdId:thirdId,
        },
        params:{
          thirdName:thirdName
        }
    })

接收参数方式同上。

2021-04-26更新(这种方式是常用的,比上述方式都好使,不过同时也存在缺陷,那就是对应的参数必须要传,否则无法跳转路由):
还有一种方式,在router.js里面定义,如
 

{
    path: '/suppliers/:keyword',
    name: 'Suppliers',
    component: () => import('@/views/suppliers/index')
  }

在路由跳转时,可配合es6语法的使用:

this.$router.push(`/suppliers/${this.keyword}`)

在接收路由参数时:

this.keyword = this.$route.params.keyword

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值