关于vue.js:VUE页面局部组件刷新

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(); //部分刷新
    }
 }

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理