vue中缓存组件keep alive的介绍及使用方法

 更新时间:2022年08月04日 09:30:18   作者:Celester_best  
这篇文章主要介绍了vue缓存组件keepalive的相关资料,keep-alive组件是使用 include exclude这两个属性传入组件名称来确认哪些可以被缓存的,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

介绍

keep-alive是vue的内置组件,可以用来缓存组件。当它包裹动态组件时,会缓存不活动的组件实例,不会销毁它们;将不活动的组件的状态保留在内存中,可以防止重复渲染DOM,减少加载事件和性能消耗。

注意:keep-alive是一个抽象组件,自身不会渲染成一个DOM元素,也不会出现在父组件链中。

原理:

在 created 函数调用时将需要缓存的 VNode 节点保存在 this.cache 中/在render(页面渲染) 时,如果 VNode 的 name 符合缓存条件(可以用 include 以及 exclude 控制),则会从 this.cache 中取出之前缓存的 VNode 实例进行渲染。

使用

缓存所有的组件

在APP.js中缓存所有组件

<template>
  <div id="app">
    <keep-alive>
      <NativeBtn>
      <router-view />
    </keep-alive>
  </div>
</template>

缓存某个组件

缓存某个组件就直接在该组件的外层嵌套一层<keep-alive>

<template>
  <div id="app">
    <!-- 只缓存NativeBtn组件 -->
    <keep-alive>
      <NativeBtn />
    </keep-alive>
    <router-view />
  </div>
</template>

keep-alive的使用示例

先来看看不加keep alive的情况

代码:

keepAliveDemo的代码

<template>
  <div>
    <button @click="changeComp">切换</button>
    <component :is="showComp" />
  </div>
</template>
<script>
import KeepAlivePageC from "./KeepAlivePageC.vue";
import KeepAlivePageB from "./KeepAlivePageB.vue";
export default {
  name: "keepAliveDemo",
  components: { KeepAlivePageC, KeepAlivePageB },
  data() {
    return {
      compType: "1",
    };
  },
  computed: {
    showComp() {
      if (this.compType === "1") {
        return KeepAlivePageC;
      } else {
        return KeepAlivePageB;
      }
    },
  },
  methods: {
    changeComp() {
      console.log("==== 点击切换按钮");
      this.compType = this.compType === "1" ? "2" : "1";
    },
  },
};
</script>

KeepAlivePageB的代码

<template>
  <div>KeepAlivePageB</div>
</template>
<script>
export default {
  name: "KeepAlivePageB",
  beforeCreate() {
    console.log(" KeepAlivePageB beforeCreate 方法执行了");
  },
  created() {
    console.log(" KeepAlivePageB created 方法执行了");
  },
  beforeMount() {
    console.log(" KeepAlivePageB beforeMount 方法执行了");
  },
  mounted() {
    console.log(" KeepAlivePageB mounted 方法执行了");
  },
  beforeUpdate() {
    console.log(" KeepAlivePageB beforeUpdate 方法执行了");
  },
  updated() {
    console.log(" KeepAlivePageB updated 方法执行了");
  },
  beforeDestroy() {
    console.log(" KeepAlivePageB beforeDestroy 方法执行了");
  },
  destroyed() {
    console.log(" KeepAlivePageB destroyed 方法执行了");
  },
};
</script>

KeepAlivePageC的代码

<template>
  <div>KeepAlivePageC</div>
</template>
<script>
export default {
  name: "KeepAlivePageC",
  beforeCreate() {
    console.log(" KeepAlivePageC beforeCreate 方法执行了");
  },
  created() {
    console.log(" KeepAlivePageC created 方法执行了");
  },
  beforeMount() {
    console.log(" KeepAlivePageC beforeMount 方法执行了");
  },
  mounted() {
    console.log(" KeepAlivePageC mounted 方法执行了");
  },
  beforeUpdate() {
    console.log(" KeepAlivePageC beforeUpdate 方法执行了");
  },
  updated() {
    console.log(" KeepAlivePageC updated 方法执行了");
  },
  beforeDestroy() {
    console.log(" KeepAlivePageC beforeDestroy 方法执行了");
  },
  destroyed() {
    console.log(" KeepAlivePageC destroyed 方法执行了");
  },
};
</script>

不使用keep alive时,切换按钮会有组件的创建和销毁

再来看下使用keep alive的情况。修改keepAliveDemo布局代码

<template>
  <div>
    <button @click="changeComp">切换</button>
    <keep-alive>
      <component :is="showComp" />
    </keep-alive>
  </div>
</template>

发现开始会有组件的创建,但是没有组件的销毁,当两个组件都创建了实例之后,再点击切换按钮,组件既不创建也不销毁,说明使用了缓存的组件实例。

include和exclude属性的使用

include:字符串或者正则表达式。只有匹配的组件会被缓存;

exclude:字符串或者正则表达式。任何匹配的组件都不会被缓存。

include的使用

只有匹配上的组件才会被缓存,没匹配上的组件不会被缓存。

修改keepAliveDemo布局代码如下

<template>
  <div>
    <button @click="changeComp">切换</button>
    <keep-alive include="KeepAlivePageC">
      <component :is="showComp" />
    </keep-alive>
  </div>
</template>

可以看到KeepAlivePageC只创建了一次,而KeepAlivePageB一直在创建和销毁

exclude的使用

匹配上的组件不会被被缓存,没匹配上的组件会被缓存。

修改keepAliveDemo布局代码如下

<template>
  <div>
    <button @click="changeComp">切换</button>
    <keep-alive exclude="KeepAlivePageC">
      <component :is="showComp" />
    </keep-alive>
  </div>
</template>

可以看到KeepAlivePageB只创建了一次,而KeepAlivePageC一直在创建和销毁

生命周期

和keep-alive相关的生命钩子是activated和deactivated

activated:被 keep-alive 缓存的组件激活时调用

deactivated:被 keep-alive 缓存的组件失活时调用

在KeepAlivePageB和KeepAlivePageC中添加activated和deactivated钩子,依然使用上面的exclude的例子

可以看到当KeepAlivePageB活动使会执行activated钩子,失活时会调用deactivated钩子

到此这篇关于vue中缓存组件keep alive的介绍及使用方法的文章就介绍到这了,更多相关vue keep alive内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue脚手架配置预渲染及prerender-spa-plugin配置方式

    vue脚手架配置预渲染及prerender-spa-plugin配置方式

    这篇文章主要介绍了vue脚手架配置预渲染及prerender-spa-plugin配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • vue如何自定义InputNumber计数器组件

    vue如何自定义InputNumber计数器组件

    这篇文章主要介绍了vue如何自定义InputNumber计数器组件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue 数据(data)赋值问题的解决方案

    vue 数据(data)赋值问题的解决方案

    这篇文章主要介绍了vue 数据(data)赋值问题的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • vue 百度地图(vue-baidu-map)绘制方向箭头折线实例代码详解

    vue 百度地图(vue-baidu-map)绘制方向箭头折线实例代码详解

    这篇文章主要介绍了vue 百度地图(vue-baidu-map)绘制方向箭头折线,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • vue spa应用中的路由缓存问题与解决方案

    vue spa应用中的路由缓存问题与解决方案

    这篇文章主要介绍了vue spa应用中的路由缓存问题与解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • vue自定义封装指令以及实际使用

    vue自定义封装指令以及实际使用

    市面上大多数关于Vue.js自定义指令的文章都在讲语法,很少讲实际的应用场景和用例,下面这篇文章主要给大家介绍了关于vue自定义封装指令以及实际使用的相关资料,需要的朋友可以参考下
    2022-01-01
  • Vue ECharts直角坐标系配置详细讲解

    Vue ECharts直角坐标系配置详细讲解

    数据的重要性我们大家都知道,就算再小的项目中都可能使用几个图表展示,我最近在做项目的过程中也是需要用到图表,最后选择了echarts图表库
    2022-12-12
  • 如何在Vue3中创建动态主题切换功能

    如何在Vue3中创建动态主题切换功能

    在Vue3中实现动态主题切换功能,通过明亮和暗色主题的选择,提供个性化使用体验,使用setup语法糖优化代码,通过创建组件和响应式变量来进行主题切换,并动态加载CSS文件
    2024-09-09
  • vue-router嵌套路由方式(页面路径跳转但页面显示空白)

    vue-router嵌套路由方式(页面路径跳转但页面显示空白)

    这篇文章主要介绍了vue-router嵌套路由方式(页面路径跳转但页面显示空白),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • vue3中如何使用iframe嵌入vue2页面

    vue3中如何使用iframe嵌入vue2页面

    这篇文章主要介绍了vue3中如何使用iframe嵌入vue2页面问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10

最新评论