• vue中缓存组件keep-alive


    目录

    介绍

    使用

    缓存所有的组件

    缓存某个组件

    keep-alive的使用示例

    include和exclude属性的使用

    include的使用

    exclude的使用

    生命周期


    介绍

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

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

    原理:

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

    使用

    缓存所有的组件

    在APP.js中缓存所有组件

    1. <template>
    2. <div id="app">
    3. <keep-alive>
    4. <NativeBtn>
    5. <router-view />
    6. </keep-alive>
    7. </div>
    8. </template>

    缓存某个组件

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

    1. <template>
    2. <div id="app">
    3. <!-- 只缓存NativeBtn组件 -->
    4. <keep-alive>
    5. <NativeBtn />
    6. </keep-alive>
    7. <router-view />
    8. </div>
    9. </template>

    keep-alive的使用示例

    先来看看不加keep alive的情况

    代码:

    keepAliveDemo的代码

    1. <template>
    2. <div>
    3. <button @click="changeComp">切换</button>
    4. <component :is="showComp" />
    5. </div>
    6. </template>
    7. <script>
    8. import KeepAlivePageC from "./KeepAlivePageC.vue";
    9. import KeepAlivePageB from "./KeepAlivePageB.vue";
    10. export default {
    11. name: "keepAliveDemo",
    12. components: { KeepAlivePageC, KeepAlivePageB },
    13. data() {
    14. return {
    15. compType: "1",
    16. };
    17. },
    18. computed: {
    19. showComp() {
    20. if (this.compType === "1") {
    21. return KeepAlivePageC;
    22. } else {
    23. return KeepAlivePageB;
    24. }
    25. },
    26. },
    27. methods: {
    28. changeComp() {
    29. console.log("==== 点击切换按钮");
    30. this.compType = this.compType === "1" ? "2" : "1";
    31. },
    32. },
    33. };
    34. </script>

    KeepAlivePageB的代码 

    1. <template>
    2. <div>KeepAlivePageB</div>
    3. </template>
    4. <script>
    5. export default {
    6. name: "KeepAlivePageB",
    7. beforeCreate() {
    8. console.log(" KeepAlivePageB beforeCreate 方法执行了");
    9. },
    10. created() {
    11. console.log(" KeepAlivePageB created 方法执行了");
    12. },
    13. beforeMount() {
    14. console.log(" KeepAlivePageB beforeMount 方法执行了");
    15. },
    16. mounted() {
    17. console.log(" KeepAlivePageB mounted 方法执行了");
    18. },
    19. beforeUpdate() {
    20. console.log(" KeepAlivePageB beforeUpdate 方法执行了");
    21. },
    22. updated() {
    23. console.log(" KeepAlivePageB updated 方法执行了");
    24. },
    25. beforeDestroy() {
    26. console.log(" KeepAlivePageB beforeDestroy 方法执行了");
    27. },
    28. destroyed() {
    29. console.log(" KeepAlivePageB destroyed 方法执行了");
    30. },
    31. };
    32. </script>

     KeepAlivePageC的代码

    1. <template>
    2. <div>KeepAlivePageC</div>
    3. </template>
    4. <script>
    5. export default {
    6. name: "KeepAlivePageC",
    7. beforeCreate() {
    8. console.log(" KeepAlivePageC beforeCreate 方法执行了");
    9. },
    10. created() {
    11. console.log(" KeepAlivePageC created 方法执行了");
    12. },
    13. beforeMount() {
    14. console.log(" KeepAlivePageC beforeMount 方法执行了");
    15. },
    16. mounted() {
    17. console.log(" KeepAlivePageC mounted 方法执行了");
    18. },
    19. beforeUpdate() {
    20. console.log(" KeepAlivePageC beforeUpdate 方法执行了");
    21. },
    22. updated() {
    23. console.log(" KeepAlivePageC updated 方法执行了");
    24. },
    25. beforeDestroy() {
    26. console.log(" KeepAlivePageC beforeDestroy 方法执行了");
    27. },
    28. destroyed() {
    29. console.log(" KeepAlivePageC destroyed 方法执行了");
    30. },
    31. };
    32. </script>

     

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

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

    1. <template>
    2. <div>
    3. <button @click="changeComp">切换</button>
    4. <keep-alive>
    5. <component :is="showComp" />
    6. </keep-alive>
    7. </div>
    8. </template>

     

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

    include和exclude属性的使用

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

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

    include的使用

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

    修改keepAliveDemo布局代码如下

    1. <template>
    2. <div>
    3. <button @click="changeComp">切换</button>
    4. <keep-alive include="KeepAlivePageC">
    5. <component :is="showComp" />
    6. </keep-alive>
    7. </div>
    8. </template>

     

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

    exclude的使用

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

    修改keepAliveDemo布局代码如下

    1. <template>
    2. <div>
    3. <button @click="changeComp">切换</button>
    4. <keep-alive exclude="KeepAlivePageC">
    5. <component :is="showComp" />
    6. </keep-alive>
    7. </div>
    8. </template>

     

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

    生命周期

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

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

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

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

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

  • 相关阅读:
    主流电商平台价格如何高频监测
    spring mvc:请求执行流程(一)之获取Handler
    计算机硬件和软件之间的区别
    MySQL - MySQL 常用存储引擎简介
    DVWA之SQL注入
    学习网络安全如何就业?
    【ChatOCR】OCR+LLM定制化关键信息抽取(附开源大语言模型汇总整理)
    Sentinel
    微信:H5如何返回至小程序
    Vue.js:渐进式JavaScript框架-前端开发
  • 原文地址:https://blog.csdn.net/Celester_best/article/details/125418259