Vue中刷新页面的方法

245 阅读1分钟

这个方法刷新页面不会出现闪烁白屏的问题,原理就是 利用v-if重新渲染dom

  1. app.vue页面
<template>
  <div id="app">
    <router-view v-if="isShow"/>
  </div>
</template>

<script>
export default {
  name: 'App',
  provide(){
    return{
      reload:this.reload
    }
  },
  data(){
    return{
      isShow:true
    }
  },
  methods:{
    reload(){
      this.isShow=false;
      this.$nextTick(()=>{
        this.isShow=true
      })
    }
  }
}
</script>
  1. 在需要刷新的页面进行引入并使用
<template>
  <div>
      <div class="header">
          <button @click="update()">刷新页面</button>
      </div>
  </div>
</template>

<script>
export default {
data(){
  return{

  }
},
inject:[
  'reload'
],
methods:{
  update(){
    this.reload()
    console.log('刷新页面')
  }
}
}
</script>