We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keep-alive
Vue全局组件
keep-alive接收三个参数:
include
字符串、正则表达式、数组
exclude
max
数字
include和exclude,传数组情况居多
数组
<keep-alive :include="allowList" :exclude="noAllowList" :max="amount"> <component :is="currentComponent"></component> </keep-alive>
<keep-alive :include="allowList" :exclude="noAllowList" :max="amount"> <router-view></router-view> </keep-alive>
前面说了,keep-alive是一个Vue全局组件,他接收三个参数:
LRU算法
顺便说说keep-alive在各个生命周期里都做了啥吧:
created
cache、keys
mounted
include、exclude
destroyed
之前说了,keep-alive不会被渲染到页面上,所以abstract这个属性至关重要!
abstract
// src/core/components/keep-alive.js export default { name: 'keep-alive', abstract: true, // 判断此组件是否需要在渲染成真实DOM props: { include: patternTypes, exclude: patternTypes, max: [String, Number] }, created() { this.cache = Object.create(null) // 创建对象来存储 缓存虚拟dom this.keys = [] // 创建数组来存储 缓存key }, mounted() { // 实时监听include、exclude的变动 this.$watch('include', val => { pruneCache(this, name => matches(val, name)) }) this.$watch('exclude', val => { pruneCache(this, name => !matches(val, name)) }) }, destroyed() { for (const key in this.cache) { // 删除所有的缓存 pruneCacheEntry(this.cache, key, this.keys) } }, render() { // 下面讲 } }
原文连接:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
keep-alive
是什么?
keep-alive
是一个Vue全局组件
keep-alive
本身不会渲染出来,也不会出现在父组件链中keep-alive
包裹动态组件时,会缓存不活动的组件,而不是销毁它们怎么用?
keep-alive
接收三个参数:include
:可传字符串、正则表达式、数组
,名称匹配成功的组件会被缓存exclude
:可传字符串、正则表达式、数组
,名称匹配成功的组件不会被缓存max
:可传数字
,限制缓存组件的最大数量include
和exclude
,传数组
情况居多动态组件
路由组件
源码
组件基础
前面说了,
keep-alive
是一个Vue全局组件
,他接收三个参数:include
:可传字符串、正则表达式、数组
,名称匹配成功的组件会被缓存exclude
:可传字符串、正则表达式、数组
,名称匹配成功的组件不会被缓存max
:可传数字
,限制缓存组件的最大数量,超过max
则按照LRU算法
进行置换顺便说说
keep-alive
在各个生命周期里都做了啥吧:created
:初始化一个cache、keys
,前者用来存缓存组件的虚拟dom集合,后者用来存缓存组件的key集合mounted
:实时监听include、exclude
这两个的变化,并执行相应操作destroyed
:删除掉所有缓存相关的东西原文连接:
The text was updated successfully, but these errors were encountered: