• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
社会优先于个人
博客园    首页    新随笔    联系   管理    订阅  订阅
vue 路由传参

params

router-link 传参

  • 在路径显示参数
  • 刷新页面参数不丢失
  • 配置路由:path:'/path/:id'
  • 获取参数: this.$route.params.id
  • 传递方式:
<router-link :to="`/path/${id}`"><router-link>

push+path

  • 在路径显示参数
  • 刷新页面参数不丢失
  • 配置路由:path:'/path/:id'
    • 获取参数: this.$route.params.id
  • 传递方式:
this.$router.push({
        path:'/zsf/222'
		//path不能和params一起使用
      })

push+name

  • 在路径不显示参数
  • 刷新页面参数丢失,是因为path没有配置id
  • 配置路由:path:'/path', name:'zsf'
  • 获取参数: this.$route.params.id
  • 传递方式:
this.$router.push({
  name:'zsf',
   params:{
           id:222
         }
})

query

router-link

  • 在路径显示参数:/path?id=123
  • 刷新页面参数不丢失
  • 配置路由:path:'/path'
  • 获取参数: this.$route.query.id
  • 传递方式
<router-link :to="{path:'/path',query:{id:'123'}}"><router-link>
//或者
<router-link :to="/path?id=123"><router-link>

push+path

  • 在路径显示参数: /path?id=222
  • 刷新页面参数不丢失
  • 配置路由:path:'/path'
    • 获取参数: this.$route.params.id
  • 传递方式:
this.$router.push({
  // path:'/zsf?id=222'
        path:'/zsf',
        query:{
          id:222
        }
})

push+name

  • 在路径显示参数:/path?id=222
  • 刷新页面参数不丢失
  • 配置路由:path:'/path', name:'zsf'
  • 获取参数: this.$route.params.id
  • 传递方式:
this.$router.push({
  name:'zsf',
   query:{
           id:222
         }
})





对象中的name+params

this.$router.push({ name: 'user', params: { id: this.id } })
路由路径不需要带:id
path:'/user'
取变量this.$route.params.id

对象中的name+query

this.$router.push({
name: 'user',
query: {
id: this.id
}
})
路由路径不需要带:id
path:'/user'
取变量this.$route.query.id

对象中的path+query

this.$router.push({
path: '/user',
query: {
id: this.id
}
})
路由路径不需要带:id
path:'/user'
取变量this.$route.query.id

字符串中不带?

this.$router.push(/user/${this.id})
路由路径需要带:id
path:'/user/:id'
取变量this.$route.params.id

字符串中带?

this.$router.push(/user?id=${this.id})
路由路径不需要带:id
path:'/user'
取变量this.$route.query.id

posted on 2022-05-05 21:24  社会优先于个人  阅读(26)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3