Vue路由传递参数的三种方式

通过动态路由方式

主要适用于传递一个参数的路由跳转,比如详情页。

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

通过params获取参数

Params只能通过name方式跳转路由。

不配置path参数

第一次获取路由参数能获取到,刷新页面后获取不到路由参数

//路由配置文件中 不配置path参数
path: "/projFeedbackDetail",
      name: "ProjFeedbackDetail",
      component: ProjFeedbackDetail

//跳转时页面
this.$router.push({
        name: "ProjFeedbackDetail",
        params: {
          prjId: this.prjId,
          cycleId: this.cycleId,
          feedbackIdKey: feedbackIdKey
        }
      });


//跳转后页面
this.prjId = this.$route.params.prjId;
this.cycleId = this.$route.params.cycleId;
this.feedbackIdKey = this.$route.params.feedba
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,可以使用路由传递参数方式有多种,下面介绍两种常用的方式: 1. 使用路由的动态路由参数(Dynamic Route Matching): 在定义路由时,可以在路由路径中使用冒号(:)来指定参数的位置。在组件中可以通过`$route.params`来获取传递的参数。 定义路由: ```javascript const router = new VueRouter({ routes: [ { path: '/user/:id', component: User } ] }); ``` 在组件中获取参数: ```javascript export default { mounted() { const userId = this.$route.params.id; // 使用参数进行相应的操作 } } ``` 使用方式: ```html <!-- 在其他地方使用router-link或者编程式导航跳转到带有参数的路由 --> <router-link :to="{ path: '/user/123' }">User</router-link> ``` 2. 使用查询参数(Query Parameters): 在定义路由时,可以使用查询参数的方式传递参数。在组件中可以通过`$route.query`来获取传递的参数。 定义路由: ```javascript const router = new VueRouter({ routes: [ { path: '/user', component: User } ] }); ``` 在组件中获取参数: ```javascript export default { mounted() { const userId = this.$route.query.id; // 使用参数进行相应的操作 } } ``` 使用方式: ```html <!-- 在其他地方使用router-link或者编程式导航跳转到带有查询参数的路由 --> <router-link :to="{ path: '/user', query: { id: '123' } }">User</router-link> ``` 以上是两种常用的在Vue传递参数方式,你可以根据实际需求选择其中一种方式来使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值