keep alive用法和原理是什么,有哪些要点
Admin 2022-08-13 群英技术资讯 1053 次浏览
很多朋友都对“keep alive用法和原理是什么,有哪些要点”的内容比较感兴趣,对此小编整理了相关的知识分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获,那么感兴趣的朋友就继续往下看吧!keep-alive可以实现组件缓存,当组件切换时不会对当前组件进行卸载。
主要是有include、exclude、max三个属性;
前两个属性允许keep-alive有条件的进行缓存;
max可以定义组件最大的缓存个数,如果超过了这个个数的话,在下一个新实例创建之前,就会将以缓存组件中最久没有被访问到的实例销毁掉。
两个生命周期activated/deactivated,用来得知当前组件是否处于活跃状态。
<template>
<div class="hello">
<h1>你怎么还在学习?️</h1>
<input placeholder="输入框" />
<router-link to="/about">我的人生理想</router-link>
</div>
</template>
<template>
<div class="hello">
<h1>我想取老婆 </h1>
</div>
</template>
<template>
<div id="app">
<router-view/>
</div>
</template>
<script>
import Home from './views/Home.vue'
import About from './views/About.vue'
export default {
components: {
Home,
About,
}
}
</script>
你在输入框中输入信息,点击跳转到另外一个页面后,回到该页面,你会发现,输入框中的文字消失了。怎么办勒

使用 keep-alive 包裹 router-view,同时指定需要缓存的组件名称。(PS:请在要缓存的组件中,写明 name 属性,并赋值。不然缓存不生效)
Home.vue

App.vue
<template>
<div id="app">
<keep-alive include="Home">
<router-view/>
</keep-alive>
</div>
</template>
<script>
import Home from './views/Home.vue'
import About from './views/About.vue'
export default {
components: {
Home,
About,
}
}
</script>

import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
// 需要被缓存
meta: {
keepAlive: true
}
},
{
path: '/about',
name: 'about',
component: () => import('./views/About.vue')
},
]
})
<script>
export default {
name: "Home",
beforeRouteLeave(to, from, next) {
to.meta.keepAlive = true;
next();
},
};
</script>
<template>
<div id="app">
<keep-alive>
<router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
<router-view v-if="!$route.meta.keepAlive"></router-view>
</div>
</template>

剩下的一些其他特性,可以自行前往官网查阅 cn.vuejs.org/v2/api/#kee…
keep-alive中运用了LRU(Least Recently Used)算法。
keep-alive 包裹着的第一个子组件对象及其组件名; 如果 keep-alive 存在多个子元素,keep-alive 要求同时只有一个子元素被渲染。所以在开头会获取插槽内的子元素,调用 getFirstComponentChild 获取到第一个子元素的 VNode。VNode),否则开启缓存策略。this.keys中的位置(更新key的位置是实现LRU置换策略的关键)。this.cache对象中存储该组件实例并保存key值,之后检查缓存的实例数量是否超过max设置值,超过则根据LRU置换策略删除最近最久未使用的实例(即是下标为0的那个key)。最后将该组件实例的keepAlive属性值设置为true。var KeepAlive = {
name: 'keep-alive',
// 抽象组件
abstract: true,
// 接收的参数
props: {
include: patternTypes,
exclude: patternTypes,
max: [String, Number]
},
// 创建缓存表
created: function created () {
this.cache = Object.create(null);
this.keys = [];
},
destroyed: function destroyed () {
for (var key in this.cache) {
pruneCacheEntry(this.cache, key, this.keys);
}
},
mounted: function mounted () {
var this$1 = this;
this.$watch('include', function (val) {
pruneCache(this$1, function (name) { return matches(val, name); });
});
this.$watch('exclude', function (val) {
pruneCache(this$1, function (name) { return !matches(val, name); });
});
},
render: function render () {
var slot = this.$slots.default;
// 获取 `keep-alive` 包裹着的第一个子组件对象及其组件名;
// 如果 keep-alive 存在多个子元素,`keep-alive` 要求同时只有一个子元素被渲染。
// 所以在开头会获取插槽内的子元素,
// 调用 `getFirstComponentChild` 获取到第一个子元素的 `VNode`。
var vnode = getFirstComponentChild(slot);
var componentOptions = vnode && vnode.componentOptions;
if (componentOptions) {
// check pattern
var name = getComponentName(componentOptions);
var ref = this;
var include = ref.include;
var exclude = ref.exclude;
// 根据设定的黑白名单(如果有)进行条件匹配,决定是否缓存。
if (
// not included
(include && (!name || !matches(include, name))) ||
// excluded
(exclude && name && matches(exclude, name))
) {
// 不匹配,直接返回组件实例(`VNode`),否则开启缓存策略。
return vnode
}
var ref$1 = this;
var cache = ref$1.cache;
var keys = ref$1.keys;
// 根据组件ID和tag生成缓存Key
var key = vnode.key == null
? componentOptions.Ctor.cid + (componentOptions.tag ? ("::" + (componentOptions.tag)) : '')
: vnode.key;
if (cache[key]) {
// 并在缓存对象中查找是否已缓存过该组件实例。如果存在,直接取出缓存值
vnode.componentInstance = cache[key].componentInstance;
// 并更新该key在this.keys中的位置(更新key的位置是实现LRU置换策略的关键)。
remove(keys, key);
keys.push(key);
} else {
// 如果不存在,则在this.cache对象中存储该组件实例并保存key值,
cache[key] = vnode;
keys.push(key);
// 之后检查缓存的实例数量是否超过max设置值,超过则根据LRU置换策略删除最近最久未使用的实例
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode);
}
}
// 最后将该组件实例的keepAlive属性值设置为true。
vnode.data.keepAlive = true;
}
return vnode || (slot && slot[0])
}
};
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本篇文章带大家了解一下nodejs中的path.join和path.resolve,介绍一下path.join和path.resolve的区别,希望对大家有所帮助!
首先需要安装模块request,然后代码如下://模拟发送http请求varrequest=require("request");//get请求request('http://www.baidu.com',function(error,response,body){if(!error&&response.statusCode==200
这篇文章主要为大家详细介绍了vue实现触底查询功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了微信小程序实现星级评分,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
简单来说,Node.js对我们项目来讲相当于一个编译环境,类似于我们java语言编译需要Jvm虚拟机一样。安装Node.js后我们就可以编译Node项目啦。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008