前端

路由跳转,回退再次进入,生命周期不调用问题

2020-05-14  本文已影响0人  一个健康马

第一种办法:
监听后进入页面会执行两次,还有其他页面也会执行,第一次不执行
加入immediate: true
第一次会调用,只调用一次

watch: {
    '$route':{
      handler(to, from) {
        if(to.path==='/pages/personCenter/media/setRecipients'&&!to.query==''){
          this.getData()
          }
        },
      immediate: true
    } 
  }

第二种办法:
<router-view :key="key" ref="routerView"/>
//绑定一个随机key 利用计算属性
computed:{
key(){
return this.$route.path + Math.random();
}
},

第三种办法:

//加v-if 销毁重组
<router-view v-if='' ref="routerView"/>

上一篇 下一篇

猜你喜欢

热点阅读