img

vue项目如何刷新当前页面?

2020-09-21 0条评论 2k次阅读 JavaScript


切换导航菜单时,希望获取新数据。
方式有很多种,今天我就介绍最实用最简单的吧,让大家少走点弯路。

首先,要修改下你的app.vue

<template>
  <div id="app">
    <router-view v-if="isRouterAlive"/>
  </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
      })
    }
  }
}

app.vue里写好reload()方法注入你需要的场景里:

如导航A菜单组件: A.vue

export default {
  inject: ['reload'],
  watch: {
    $route (to, from) {
      if (to.path === '/path/A.vue') {
        this.reload()
      }
    }
  },
}

搞定了!

💬 COMMENT


🦄 支持markdown语法

👋友