keep-alive指定缓存或不缓存某些组件时不生效

目录

一、问题

二、解决方法

三、总结


tiips:如嫌繁琐,直接移步总结即可!

一、问题

1.有些页面的数据加载太慢了,需要缓存;而有些地方需要实时调用接口才能够拿到最新的数据;甚至有些地方需要另外一个缓存的数据才能调用接口获取数据。

二、解决方法

1.keep-alive可以缓存组件,可是我有些地方需要缓存,有些地方不需要缓存。怎么办呢?百度了半天,毫无头绪。看到include属性,添加后发现依然无效。

2.不得不去官网看看,到底是怎么用的。

3.官网是这样写的:提供include属性时,只有与include名称匹配的组件会被缓存。

4.我真的很纳闷呀,我也是这样写的,怎么不起作用呢?我也是这样写的呀。。。。。

javascript 复制代码
<template>
  <keep-alive :include="'EditNurse'">
    <component is="currentComponent"> </component>
  </keep-alive>
</template>
<script>
import { defineComponent, toRefs, ref, computed, watch, getCurrentInstance, onMounted, reactive } from 'vue';
import EditNurse from './EditTemplate.view';
export default defineComponent({
  components: {
    EditNurse
  },
  setup() {
    const currentComponent = ref('EditNurse');
    return {
      currentComponent
    };
  }
});
</script>

5.组件名称?好像组件都有一个name属性,试试在 EditNurse组件中加一个name说不定可以。。。看了一下EditNurse组件发现已经有name: 'EditTemplate',了,把上面include换成 EditTemplate就好了。

javascript 复制代码
<template>
  <keep-alive :include="'EditTemplate'">
    <component is="currentComponent"> </component>
  </keep-alive>
</template>
<script>
import { defineComponent, toRefs, ref, computed, watch, getCurrentInstance, onMounted, reactive } from 'vue';
import EditNurse from './EditTemplate.view';
export default defineComponent({
  components: {
    EditNurse
  },
  setup() {
    const currentComponent = ref('EditNurse');
    return {
      currentComponent
    };
  }
});
</script>

6.然后我意识到怎么会这么无厘头呢,为啥引入的组件要换名称呢?就算引入的时候换了名称,对应的组件name不应该也相应修改吗?

7.测试了一下两种情况

1)EditTemplate组件内部不显示指定 name: 'EditTemplate', 引入组件后重命名为 EditNurse,直接使用 4中的代码:

:include="'EditNurse'"是正确的,组件EditTemplate可以被缓存。

:include="'EditTemplate'"是错误的,组件EditTemplate不可以被缓存。

结论:组件内部没有显示提供name时,引入的时候名称是什么,则组件的name就是什么。

2)引入后的组件名称和引入前一样EditTemplate。include的时候当然也是用的EditTemplate,正确,可以被缓存。

javascript 复制代码
<template>
  <keep-alive :include="'EditTemplate'">
    <component is="currentComponent"> </component>
  </keep-alive>
</template>
<script>
import { defineComponent, toRefs, ref, computed, watch, getCurrentInstance, onMounted, reactive } from 'vue';
import EditTemplate from './EditTemplate.view';
export default defineComponent({
  components: {
    EditTemplate
  },
  setup() {
    const currentComponent = ref('EditTemplate');
    return {
      currentComponent
    };
  }
});
</script>

8.最终结论:原始组件内部没有指明 name时,引入的时候叫什么名字,则在当前文件下面该组件的name就是什么。 原始组件内部有name时,无论你引入后交什么名字,在当前文件下面该组件的name都是原始组件中的name。

三、总结

1.keep-alive include不生效请检查原始组件内部是否显示声明 name,如果有,则include中的内部必须是原始组件中的name;如果没有,则你引入组件的时候叫什么名称,include中就使用什么名称。

javascript 复制代码
//1.原始组件B有name

import A from './B.vue';

B组件: name:'componentB'

:include="'componentB'"

//2.原始组件B没有name

import A from './B.vue';

:include="'A'"

2.exclude属性也是和上面的include属性一样的!

3.诶,天天采坑,是祸也是福,解决了就是福!

/*

希望对你有帮助!

如有错误,欢迎指正,非常感谢!

*/

相关推荐
大猫子的技术日记20 小时前
[百题重刷]前缀和 + Hash 表:缓存思想, 消除重复计算
java·缓存·哈希算法
愤怒的山羊21 小时前
jetcache List 缓存, json 序列化 泛型解析成了 JsonObject 处理
缓存·json·list
树在风中摇曳1 天前
带哨兵位的双向循环链表详解(含 C 代码)+ LeetCode138 深度解析 + 顺序表 vs 链表缓存机制对比(图解 CPU 层级)
c语言·链表·缓存
斯文~1 天前
「玩透ESA」站点配置阿里云ESA全站加速+自定义规则缓存
阿里云·缓存·云计算·cdn·esa
S***t7141 天前
Python装饰器实现缓存
缓存
天硕国产存储技术站1 天前
3000次零失误验证,天硕工业级SSD筑牢国产SSD安全存储方案
缓存·固态硬盘·国产ssd
前端炒粉1 天前
35.LRU 缓存
开发语言·javascript·数据结构·算法·缓存·js
努力发光的程序员2 天前
互联网大厂Java面试:从Spring Boot到微服务架构
spring boot·缓存·微服务·消息队列·rabbitmq·spring security·安全框架
zero13_小葵司2 天前
JavaScript性能优化系列(八)弱网环境体验优化 - 8.3 数据预加载与缓存:提前缓存关键数据
javascript·缓存·性能优化
CS_浮鱼2 天前
【Linux进阶】mmap实战:文件映射、进程通信与LRU缓存
linux·运维·c++·缓存