vue-router的参数传递

vue-router的参数传递方式分为两种:

  • query
  • params

举例说明

1.query

传参

   this.$router.push({
     path: "/arg",
     query: {
        id: 1,
        name: "nicoz",
     },
   });

获取

console.log(this.$route.query.id)
console.log(this.$route.query.name)

浏览器地址

 总结特点:通过query传递的参数会显示在浏览器地址后面,刷新界面后不会消失

2.params

传参

  this.$router.push({
        name: "HelloWorld",
        params: {
          id: 2,
          name: "nini",
        },
   });

获取

console.log("id:" + this.$route.params.id);
console.log("name:" + this.$route.params.name);

浏览器地址

 总结特点:通过params传参参数不会显示在浏览器地址后面,并且刷新界面后会消失

|-解决params传参刷新界面后消失的问题:

在router.js的声明文件中将参数声明在path中

  {
      path: '/hello/:id/:name',
      name: 'HelloWorld',
      component: HelloWorld
    },

浏览器地址

posted @   南无、  阅读(4649)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示