vue路由每次访问相同的路由,都会刷新

转自:https://www.zhihu.com/question/49863095/answer/289157209

参考文档:

https://blog.csdn.net/qq_39009348/article/details/81700082

作者:知乎用户
链接:https://www.zhihu.com/question/49863095/answer/289157209
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 

利用v-if控制router-view,在路由容器组件,如APP.vue中实现一个刷新方法

<template>
  <router-view v-if="isRouterAlive"/>
</template>
<script>
export default {
 data () {
   return {
     isRouterAlive: true
   }
 },
 methods: {
   reload () {
     this.isRouterAlive = false
     this.$nextTick(() => (this.isRouterAlive = true))
   }   
 }
}
</script>

然后其它任何想刷新自己的路由页面,都可以这样:

this.$root.reload()

如果$root节点不是路由容器组件,可以使用provide / inject来传递reload

扫描二维码关注公众号,回复: 3925126 查看本文章

路由容器组件:

<template>
  <router-view v-if="isRouterAlive"/>
</template>
<script>
export default {
 provide () {
    return {
      reload: this.reload
    }
  },
  data () {
    return {
      isRouterAlive: true
    }
  },
  methods: {
    reload () {
      this.isRouterAlive = false
      this.$nextTick(() => (this.isRouterAlive = true))
    }
  }
}
</script>

子组件:

...
{
  inject:  ['reload']
}
...
this.reload()

猜你喜欢

转载自blog.csdn.net/zhangshab/article/details/82751535
今日推荐