Vue路由传参的几种方式详解

本文详细介绍了VueRouter中的参数传递方法,包括编程式导航的字符串和对象方式,以及命名路由和查询参数的使用。强调了命名路由与查询参数的区别,以及在页面刷新时参数的处理。同时提到了声明式导航的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

查看本专栏目录


关于作者

还是大剑师兰特:曾是美国某知名大学计算机专业研究生,现为航空航海领域高级前端工程师;优快云知名博主,GIS领域优质创作者,深耕openlayers、leaflet、mapbox、cesium,canvas,webgl,echarts等技术开发,欢迎加底部微信,一起交流。

热门推荐内容链接
1openlayers 从基础到精通,300+代码示例
2leaflet 热门分解学习教程,150+图文示例
3cesium 从0到1学习指南,200+代码示例
4 mapboxGL 从入门到实战,150+图文示例
5canvas 示例应用100+,揭密底层细节
6javascript从基础到高级,示例展示200+
7vue2 实战指南,100+个细节深度剖析

在这里插入图片描述

vue-router传递参数分为两大类:

编程式的导航 router.push
声明式的导航

在这里插入图片描述

编程式导航

1)编程式导航传递参数有两种类型:字符串、对象

字符串

字符串的方式是直接将路由地址以字符串的方式来跳转,这种方式很简单但是不能传递参数:
this.$router.push(“home”);

对象

想要传递参数主要就是以对象的方式来写,分为两种方式:命名路由、查询参数,下面分别说明两种方式的用法和注意事项。

命名路由
命名路由的前提就是在注册路由的地方需要给路由命名如:

命名路由传递参数需要使用params来传递,这里一定要注意使用params不是query。目标页面接收传递参数时使用params。特别注意:命名路由这种方式传递的参数,如果在目标页面刷新是会出错的

使用方法如下:
this.$router.push({ name: ‘news’, params: { userId: 123 }})
代码如下:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button @click="routerTo">click here to news page</button>
  </div>
</template>
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  methods:{
    routerTo(){
      this.$router.push({ name: 'news', params: { userId: 123 }});
    }
  }
}
</script>
 
<style>
</style>
 
接受传递的参数:
<template>
  <div>
    this is the news page.the transform param is {{this.$route.params.userId}}
  </div>
</template>
<script>
  
</script>

查询参数
查询参数其实就是在路由地址后面带上参数和传统的url参数一致的,传递参数使用query而且必须配合path来传递参数而不能用name,目标页面接收传递的参数使query。
注意:和name配对的是params,和path配对的是query
使用方法如下:
this.$router.push({ path: ‘/news’, query: { userId: 123 }});
代码如下:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button @click="routerTo">click here to news page</button>
  </div>
</template>
<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  methods:{
    routerTo(){
      this.$router.push({ path: '/news', query: { userId: 123 }});
    }
  }
}
</script>
<style>
</style>
 
接收参数如下:
<template>
  <div>
    this is the news page.the transform param is {{this.$route.query.userId}}
  </div>
</template>
<script>
</script>

运行效果如下:

声明式的导航

字符串
<router-link to="news">click to news page</router-link>
命名路由
<router-link :to="{ name: 'news', params: { userId: 1111}}">click to news page</router-link>
查询参数
<router-link :to="{ path: '/news', query: { userId: 1111}}">click to news page</router-link>

最后总结:路由传递参数和传统传递参数是一样的,命名路由类似表单提交而查询就是url传递,在vue项目中基本上掌握了这两种传递参数就能应付大部分应用了,最后总结为以下两点:
1.命名路由搭配params,刷新页面参数会丢失
2.查询参数搭配query,刷新页面数据不会丢失
3.接受参数使用this.$router后面就是搭配路由的名称就能获取到参数的值

专栏目标

在vue和element UI联合技术栈的操控下,本专栏提供行之有效的源代码示例和信息点介绍,做到灵活运用。

提供vue2的一些基本操作:安装、引用,模板使用,computed,watch,生命周期(beforeCreate,created,beforeMount,mounted, beforeUpdate,updated, beforeDestroy,destroyed,activated,deactivated,errorCaptured,components,)、 $root , $parent , $children , $slots , $refs , props, $emit , eventbus ,provide / inject, Vue.observable, $listeners, $attrs, $nextTick , v-for, v-if, v-else,v-else-if,v-on,v-pre,v-cloak,v-once,v-model, v-html, v-text, keep-alive,slot-scope, filters, v-bind,.stop, .native, directives,mixin,render,国际化,Vue Router等

.js中使用TypeScript进行路由传参时,有几种方式可以实现。引用中的代码示例展示了一种常见的方式,即使用params传参方式。在router/index.ts文件中,可以配置路由的name属性,如name:"Reg"。然后在商品列表页面使用router.push方法进行路由跳转,并将参数通过params传递,例如router.push({ name:"Reg", params:item }),其中item是一个对象,包含了需要传递的参数。在详情页面的模板中,可以使用route.params来获取传递过来的参数,如{{ route.params.name }}来获取商品名称。同时,可以使用$router.back方法返回上一页。需要注意的是,为了使用useRoute和useRouter方法,需要在script标签中使用setup函数,并导入相应的模块。这样就可以实现在Vue.js中使用TypeScript进行路由传参了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue路由传参 vue路由传参](https://download.csdn.net/download/weixin_39709920/87065136)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Vue3 / Ts Router详解](https://blog.csdn.net/Royzilong/article/details/123736090)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

还是大剑师兰特

打赏一杯可口可乐

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

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

打赏作者

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

抵扣说明:

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

余额充值