关于vue router 传参获取不到问题

版权声明:本文为博主原创文章,未经博主允许不得转载。github仓库:https://github.com/lzcwds,欢迎访问 https://blog.csdn.net/lzcwds/article/details/81777042

在当前路由中有一个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,即在浏览器地址栏中不显示参数。

所以以上带面做下面这样的修改就可以获取数据:

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

猜你喜欢

转载自blog.csdn.net/lzcwds/article/details/81777042