Vue Router传参失败?如何有效排查并修复?

更新于
2026-09-28 10:53:03
0阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计283个文字,预计阅读时间需要2分钟。

Vue Router传参失败?如何有效排查并修复?

在当前路由中,存在一个toArticle方法可以跳转到article页面。方法如下:javascriptmethods: { toArticle: function(index) { this.$router.push({ path: '/article', params: this.blogList[index] }); }}在article页面中,无法接收到params参数。在mounted生命周期钩子中,控制台输出:javascriptconsole.log(this.$route.params);

在当前路由中有一个toArticle方法可以跳转到article页面

methods:{ toArticle:function(index) { this.$router.push({path:'/article',params:this.blogList[index]}); } }

在article中接受不到params

mounted(){ console.log(this.$route.params) //这里输出undifined }

导致这样的原因是因为params需要通过name来获取,这里就要明白query和params的区别了

query要用path来引入,接收参数都是this.$route.query.name。query类似于ajax中get传参,即在浏览器地址栏中显示参数。

params要用name来引入,接收参数都是this.$route.params.name。params则类似于post,即在浏览器地址栏中不显示参数。

阅读全文

本文共计283个文字,预计阅读时间需要2分钟。

Vue Router传参失败?如何有效排查并修复?

在当前路由中,存在一个toArticle方法可以跳转到article页面。方法如下:javascriptmethods: { toArticle: function(index) { this.$router.push({ path: '/article', params: this.blogList[index] }); }}在article页面中,无法接收到params参数。在mounted生命周期钩子中,控制台输出:javascriptconsole.log(this.$route.params);

在当前路由中有一个toArticle方法可以跳转到article页面

methods:{ toArticle:function(index) { this.$router.push({path:'/article',params:this.blogList[index]}); } }

在article中接受不到params

mounted(){ console.log(this.$route.params) //这里输出undifined }

导致这样的原因是因为params需要通过name来获取,这里就要明白query和params的区别了

query要用path来引入,接收参数都是this.$route.query.name。query类似于ajax中get传参,即在浏览器地址栏中显示参数。

params要用name来引入,接收参数都是this.$route.params.name。params则类似于post,即在浏览器地址栏中不显示参数。

阅读全文