首页 > 技术文章 > vue强制刷新页面

niuben 原文

方法一

this.$router.go(0) // 会出现一段空白页,用户体验不好

方法二

app.vue 中定义 reload() 方法

<template>
  <div id="app">
    <router-view v-if="isReload"/>
  </div>
</template>
 
<script>
export default {
  name: 'App',
  provide() {
    return {
      reload: this.reload
    }
  },
  data() {
    return {
      isReload: true
    }
  },
  methods: {
    reload() {
      this.isReload = false
      this.$nextTick(() => {
        this.isReload = true
      })
    }
  }
}
</script>

在需要强制刷新的页面引用

<script>
export default {
  inject: ['reload'],
  methods: {
    clickReload() { // 点击之后强制刷新
       this.reload()
     }
  }
}
</script>

推荐阅读