首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ JavaScript / 文章详情

vue 路由参数变化,页面不刷新(数据不更新)解决方法

Sonder
2020-09-30
769字
2分钟
浏览 (3.6k)

路由参数改变,页面数据不更新解决方法:

复制代码
http://localhost:8080/#test?id=1

http://localhost:8080/#test?id=2

参数切换后,数据未更新

以下为解决办法:

监听路由变化,若参数不相同,则重新加载数据,更新视图:

复制代码
watch: {
     '$route' (to, from) { //监听路由是否变化
         if(to.query.id != from.query.id){
             this.id = to.query.id;
             this.init();//重新加载数据
         }
     }
},
created() {
   if(this.$route.query) {
       this.id = this.$route.query.id;
       this.init();
   }
},

路由跳转也可以通过 watch 这样解决

复制代码
 watch: {
    $route(to) {
      this.routerName = to.name;
      this.routerBelongName = to.meta.belongGroup;
      console.log(this.routerBelongName,"watch")
    }
  },
  mounted() {
    this.routerName = this.$route.name;
    this.routerBelongName = this.$route.meta.belongGroup;
  }
下一篇 / 前端下载外部网络图片

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)