vue实现页面刷新

在我的页面里有使用过三种页面刷新的方法,接下来挨个介绍下:

一、当前窗口刷新

window.location.reload() //页面刷新

二、路由切换方式

this.$router.push("需要刷新的页面地址"); //页面刷新

以上两种方式都可以有页面刷新的效果,但是缺点就是会出现空白页,第三种方式就可以解决这一缺点,我们一起来看看。。。

三、provide / inject 组合方式

首先在你的App.vue页面添加v-if=“isRouterAlive”,如以下代码:

<template>
  <div>
    <router-view v-if="isRouterAlive"></router-view>
  </div>
</template>
export default {
  name: "app",
  data() {
    return {
      isRouterAlive: true,
    };
  },
  provide() {
    //提供
    return {
      reload: this.reload,
    };
  },
  methods: {
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      });
    },
  },
};

最后在你需要加载的页面注入App.vue组件提供(provide)的 reload 依赖,然后直接用this.reload来调用就行,如以下代码:

  inject: ["reload"], //注入   和methods同级
  methods: {
    onSubmit() {
       this.reload(); //局部刷新
    }
 }

逆风微笑的代码狗
30 声望1 粉丝