前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue路由传参的两种方式

Vue路由传参的两种方式

作者头像
陶然同学
发布2023-02-27 09:55:23
2540
发布2023-02-27 09:55:23
举报
文章被收录于专栏:陶然同学博客

在vue中,参数传递共有2种:

http://localhost:8080/demo?cid=c001

http://localhost:8080/demo2/c001

查询参数

  • 确定访问路径

<!-- 带查询参数,下面的结果为 /demo?cid=c001 --> <router-link to="/demo?cid=c001">Demo</router-link> | <router-link :to="{ path: '/demo', query: { cid: 'c001' }}">demo</router-link>

  • 编写路由

  {     path: '/demo',     name: 'demo',     component: Demo   },

  • 编写页面,并获得参数

<template> <div> </div> </template> <script> export default {     mounted() { // 获得查询参数         console.info( this.$route.query.cid )     }, } </script> <style> </style>

路径参数

  • 编写访问路径

<router-link :to="{ name: 'demoName2', params: { uid: 123 }}">Demo2</router-link> | <router-link to="/demo2/123">Demo2</router-link> |

  • 确定访问路径

  {     path: '/demo2/:uid',     name: 'demoName2',     component: Demo2   }

  • 编写路由

{     path: '/demo2/:uid',     name: 'demoName2',     component: Demo2   }

  • 编写页面,并获得参数

<template> <div> </div> </template> <script> export default {     mounted() { // 获得路径参数         console.info( this.$route.params.uid )     }, } </script> <style> </style>

路由总结

  • 在任何组件内通过 this.$router 访问路由器
  • 在任何组件内通过 this.$route 访问当前路由:

this.$route相关操作

描述

实例

this.$route.params

获得路径参数

模式

/user/:uid

匹配路径

/user/123

this.$route.params.uid

123

this.$route.query

获得请求参数

/user?id=123

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 查询参数
  • 路径参数
  • 路由总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档