美文网首页
Vue 刷新当前页面

Vue 刷新当前页面

作者: _凌浩雨 | 来源:发表于2018-08-17 18:16 被阅读3002次
1). location方式
location.reload()
2). router方式
this.$router.go(0)
3). provide/inject方式
  • App.vue
<script>
  export default {
    name: 'App',
    // 提供reload方法
    provide: function () {
      return {
        reload: this.reload
      }
    },
    // isRouterAlive控制显示
    data: function () {
      return {
        isRouterAlive: true
      }
    },
    methods: {
      // 刷新方法
      reload: function () {
        this.isRouterAlive = false;
        // 该方法会在dom更新后执行
        this.$nextTick(function () { this.isRouterAlive = true })
      }
    }
  }
</script>
  • home.vue
<script>
  export default {
    name: 'home',
    // 注入reload, AppVue中注册
    inject: ['reload'],
    methods: {
      // 退出登陆
      logout: function () {
        // 刷新
        // location.reload()
        // this.$router.go(0)
        // 刷新当前页面
        this.reload();
      }
    }
  }
</script>

效果演示:


效果.gif

相关文章