当前位置:网站首页>vue中缓存组件keep-alive
vue中缓存组件keep-alive
2022-06-26 19:37:00 【Celester_best】
目录
介绍
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钩子
边栏推荐
- Pinda general permission system (day 1~day 2)
- 知識點總結
- 品达通用权限系统(Day 1~Day 2)
- Unity - URP get camera stack
- The king of Internet of things protocol: mqtt
- xlua获取ugui的button注册click事件
- Redis单点登陆系统+投票系统
- Analysis on development technology of NFT meta universe chain game system
- Résumé des points de connaissance
- Why don't I recommend going to sap training institution for training?
猜你喜欢
随机推荐
股票开户的具体步骤是什么?网上开户安全吗?
Is it safe to open an account for CICC Wealth Online?
Union, intersection and difference operations in SQL
Summary of knowledge points
Request method 'POST' not supported
转:实事求是
Tree array
【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)
On the escape of inequality value
Good thing recommendation: mobile terminal development security tool
8VC Venture Cup 2017 - Final Round C. Nikita and stack
Tiktok practice ~ homepage video ~ pull-down refresh
Kubernetes 资源拓扑感知调度优化
Tiktok practice ~ search page ~ video details
Micro service single sign on system (SSO)
【Mysql系列】工作常用sql集锦(持续更新)
50 lines of code to crawl TOP500 books and import TXT documents
ImageView, glide load long picture (glide load picture)
网上办理中金财富开户安全吗?
Using recursion to find all gray codes with n bits









