美文网首页
Vue刷新当前路由的方式(2)

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

作者: Sven0706 | 来源:发表于2018-11-14 11:29 被阅读0次

这是之前使用的方法,使用的是跳转空白页再跳转回来的方式,虽然也能刷新当前路由,但是还是存在一些瑕疵的
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)

相关文章

网友评论

      本文标题:Vue刷新当前路由的方式(2)

      本文链接:https://www.haomeiwen.com/subject/fbbdfqtx.html