Vue刷新当前路由的方式(2)

这是之前使用的方法,使用的是跳转空白页再跳转回来的方式,虽然也能刷新当前路由,但是还是存在一些瑕疵的
Vue刷新当前路由的方式(1)
所有这里推荐新的方法,并且算是终极解决方法了吧

provide /inject组合实现
原理:允许一个祖先组件向其所有子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效

App.vue,声明reload方法,控制router-view的显示或隐藏,从而控制页面的再次加载。

<template>
  <div id="app">
    <router-view v-if="isRouterAlive"></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
  provide () {
    return {
      reload: this.reload
    }
  },
  data () {
    return {
      isRouterAlive: true
    }
  },
  methods: {
    reload () {
      this.isRouterAlive = false
      this.$nextTick(function () {
        this.isRouterAlive = true
      })
    }
  }
}
</script>

在需要用到刷新的页面。在页面注入App.vue组件提供(provide)的 reload 依赖,在逻辑完成之后(删除或添加...),直接this.reload()调用,即可刷新当前页面。

在组件内注入reload方法

<script>
export default {
  inject:['reload'],
  data(){
    return {
       ... 
    }
  }
}
</script>

之后就是在需要刷新的时候调用this.reload()方法了

Vue刷新当前路由的方式(1)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。