Blogger Information
Blog 87
fans 1
comment 0
visits 58848
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue路由传参的三种方式
阿杰
Original
771 people have browsed it

主要分params传参和query传参

方法一:params传参(显示参数)

1、声明式:router-link

  1. //子路由配置
  2. {
  3. path'/child/:id',
  4. component: Child
  5. }
  6. //父路由组件
  7. <router-link :to='/child/123'></router-link>

2、编程式 this.$router.push

  1. //子路由配置
  2. {
  3. path'/child/:id',
  4. component: Child
  5. }
  6. //父路由编程式传参
  7. this.$router.push({
  8. path:'/child/${id},
  9. })

子路由可以通过下面代码获取传递过来的参数

  1. this.$route.params.id

方法二:params传参(不显示参数)

1、声明式:router-link

  1. <router-link :to="{name:'Child',params:{id:123}}">进入Child路由</router>

2、编程式:this.$router.push

  1. //子路由配置
  2. {
  3. path:'/child',
  4. name:'Child',
  5. component:Child
  6. }
  7. //父路由编程式传参
  8. this.$router.push({
  9. name:'Child',
  10. params:{
  11. id:123
  12. }
  13. })

子路由可以通过下面代码获取传递过来的参数

  1. this.$route.params.id

注意:上述这种利用 params 不显示 url 传参的方式会导致在刷新页面的时候,传递的值会丢失
(vue官方在2022-08-22做了修改,使用params传参是如果,路径上没有:xx参数,params将会失效。解决办法也很简单:)

方法三:quaery传参(显示参数)

1、声明式:router-link

  1. <router-link :to="{name:'Child',query:123}}">进入Child路由</router>

2、编程式:this.$router.push

  1. //子路由配置
  2. {
  3. path:'/child',
  4. name:'Child',
  5. component:Child
  6. }
  7. //父路由编程式传参
  8. this.$router.push({
  9. name:'Child',
  10. query:{
  11. id:123
  12. }
  13. })

子路由可以通过下面代码获取传递过来的参数

  1. this.$route.query.id
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post