this.$route.query和this.$route.params的详解

一、this.$route.query的使用

1、router/index.js 文件

复制代码
{ 
 path:'/bms',
 component: bms,
 //添加路由
 children:[
   {
    path:':shopid',
    component:guessdetail
   }
 ]     
}
复制代码

2、传参

this.$router.push({
        path: '/bms/detail', 
        query:{
          Id: id
         }
        });

3、获取参数

this.$route.query.Id

案例:

复制代码
// 传参
TitleManagement(row) {
  this.$router.push({
    path: "/bms/topicList",
    query: {
      id: row.id
    },
  });
}
// 获取参数
this.questionBankId = this.$route.query.id;
复制代码

4、url的表现形式(url中带有参数)

http://localhost:8080/#/mtindex/detail?Id=1

PS: 页面之间用路由跳转传参时,刷新跳转后传参的页面,数据还会显示存在

二、this.$route.params的使用

1、router/index.js 文件

复制代码
{
 path:'/bms',
 component: bms,
 //添加路由
 children:[
   {
    path:"/detail",
    name:'detail',
    component:guessdetail
   }
 ]     
}
复制代码

2、传参数( params相对应的是name query相对应的是path)

this.$router.push({
  name: 'detail', 
  params:{
  Id: id
  }
});

3、获取参数

this.$route.params.Id

4、url的表现形式(url中没带参数)

http://localhost:8080/#/mtindex

PS: 页面之间用路由跳转传参时,刷新跳转后传参的页面,数据不存在

三、this.$router与this.$route区别与联系

$router : 是路由操作对象,只写对象

$route : 路由信息对象,只读对象

1,、this.$router是Vue Router的实例,包含了一些路由的跳转方法,钩子函数等.

想要导航到不同的url,则使用this.$router.push()  $router对象是全局路由的实例,是router构造方法的实例

路由实例方法:

  • push(): push方法的跳转会向 history 栈添加一个新的记录,当我们点击浏览器的返回按钮时可以看到之前的页面。
  • go(): 页面路由跳转 前进或者后退
  • replace(): push方法会向 history 栈添加一个新的记录,而replace方法是替换当前的页面, 不会向 history 栈添加一个新的记录。 一般使用replace来做404页面

2、this.$route对象表示当前的路由信息,包含了当前url解析得到的信息,包含当前的路径、参数、query对象等

posted @   周橘  阅读(4015)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示