vue页面刷新当前页的方法

关于vue项目刷新当前页面,我试了一些方法,最终找到了一个目前我所知道最棒的方法。
记录如下:
在App.vue声明reload方法,控制router-view的显示或隐藏,从而控制页面的再次加载

在App.vue页面代码:

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

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

</script>

在使用页面,需要引入在App.vue页面定义的reload方法

  inject:['reload'],       //注入reload方法

使用此方法:

// 退出登录
    logout() {
      // sessionStorage清零
      window.sessionStorage.clear()
      // 页面刷新
      this.reload()
    },
发布了1 篇原创文章 · 获赞 1 · 访问量 186

猜你喜欢

转载自blog.csdn.net/sinat_42684420/article/details/103994962