路径:

由上图可知,keep-alive.js导出的对象(如下),作为一个属性将会注入到Vue.options.components中。

const patternTypes = [String, RegExp, Array]
export default {
name: 'keep-alive',
abstract: true, props: {
// 根据组件 name 进行匹配。如果 name 选项不可用,则匹配它的局部注册名称 (父组件 components 选项的键值)。匿名组件不能被匹配。
include: patternTypes, // 匹配的组件将会被缓存
exclude: patternTypes, // 匹配的组件不会被缓存
max: [String, Number] // 最大缓存组件数
}, created () {
// 保存缓存的组件
this.cache = Object.create(null)
// 保存缓存的组件的key
this.keys = []
}, // 注销钩子函数
destroyed () {
for (const key in this.cache) {
pruneCacheEntry(this.cache, key, this.keys)
}
}, watch: {
include (val) {
pruneCache(this, name => matches(val, name))
},
exclude (val) {
pruneCache(this, name => !matches(val, name))
}
}, render () {
// this.$slots.default 包含了所有没有被包含在具名插槽中的节点
// 这里取得第一个子组件
const vnode = getFirstComponentChild(this.$slots.default)
const componentOptions = vnode && vnode.componentOptions if (componentOptions) {
// check pattern
const name = getComponentName(componentOptions)
// 如果 componentName 没有作为keep-alive被包含进来,直接返回
if (name && (
(this.include && !matches(this.include, name)) ||
(this.exclude && matches(this.exclude, name))
)) {
return vnode
} const { cache, keys } = this
const key = vnode.key == null
// 相同的构造器(constructor)可能会注册为不同的本地组件,所以仅有一个 cid 是不够的(#3269)。
? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
: vnode.key if (cache[key]) { // 如果已经缓存了,需要保持当前的key 是最新的
vnode.componentInstance = cache[key].componentInstance
remove(keys, key)
keys.push(key)
} else { // 否则,进行缓存并更新keys数组。
cache[key] = vnode
keys.push(key)
// 缓存组件超出最大值,将队首的组件销毁(先进先出)
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
}
} vnode.data.keepAlive = true
}
return vnode
}
}
<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>

如何实现keep-alive的更多相关文章

  1. SQL Server 2012故障转移的looksalive check和is alive check

    什么是looksalive check和is alive check SQL Server故障转移集群是建立在windows集群服务上的一种热备的高可用方案.在集群运行过程中,windows集群服务定 ...

  2. Keeping Async Methods Alive

    Consider a type that will print out a message when it’s finalized, and that has a Dispose method whi ...

  3. 转载:有关SQL server connection Keep Alive 的FAQ(3)

    转载:http://blogs.msdn.com/b/apgcdsd/archive/2012/06/07/sql-server-connection-keep-alive-faq-3.aspx 这个 ...

  4. 转载:有关SQL server connection Keep Alive 的FAQ(2)

    转: http://blogs.msdn.com/b/apgcdsd/archive/2012/05/18/sql-server-connection-keep-alive-faq-2.aspx 在下 ...

  5. Keep Alive

    跳板机时经常出现连接被断开的情况.如果发生这种情况,请在客户端配置Keep Alive设置,具体方法参考如下: Windows: secureCRT:Properties -> Terminal ...

  6. How to Keep Alive SSH Sessions

    How to Keep Alive SSH Sessions Many NAT firewalls time out idle sessions after a certain period of t ...

  7. keep alive的相关介绍

        无论Windows还是linux,Keepalive就三个配置参数.下文以linux环境为介绍. Technorati 标签: keepalive     tcp_sock结构体中有三个有关的 ...

  8. linux下socket keep alive讲解

    [需求] 不影响服务器处理的前提下,检测客户端程序是否被强制终了.[现状]服务器端和客户端的Socket都设定了keepalive属性.服务器端设定了探测次数等参数,客户端.服务器只是打开了keepa ...

  9. [Server Running] [Node.js, PM2] Using PM2 To Keep Your Node Apps Alive

    PM2 is a production process manager for Node.js applications with a built-in load balancer. It allow ...

  10. 【转】Linux下socket keep alive讲解

    [需求]不影响服务器处理的前提下,检测客户端程序是否被强制终了.[现状]服务器端和客户端的Socket都设定了keepalive属性.服务器端设定了探测次数等参数,客户端.服务器只是打开了keepal ...

随机推荐

  1. python不用声明数据类型

    不用声明变量一样,Python不用去声明函数的返回类型,是由于其“若类型”的语言特性决定的. 在其他语言中,例如C/C++语言中在存储一个数据之前,都需要在内存中给这个数据开辟一个固定的内存空间,并给 ...

  2. 我所理解的 Laravel 请求 生命周期

    转载自:https://laravel-china.org/topics/3343/my-understanding-of-the-laravel-request-life-cycle 当你使用一个工 ...

  3. vue cli+axios踩坑记录+拦截器使用,代理跨域proxy(更新)

    16319 1.首先axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点建议方式 在main.js中如下声明使用 import axios from 'ax ...

  4. FTP 搭建

    FTP 搭建 FTP 是 File Transfer Protocol(文件传输协议)的英文简称,它工作在 0SI 模型的第七层,TCP 模型的第四屋上,即应用层. 一.FTP 简介 FTP 会话时包 ...

  5. PDF,word ,PPT,等各种文件转换在线工具(免费)

    https://smallpdf.com

  6. Docker swarm结合Openresty部署rabbitmq集群

    Docker swarm结合Openresty部署rabbitmq集群 大家好,年底了,年味儿越来越浓了.2019年的寒冬被定义为未来10年中最好的一年,对于这一说法悲观的人和乐观的人的理解是不一样的 ...

  7. jdk1.7和1.8共存的问题(默认1.7)

    参考https://www.cnblogs.com/fxmemory/p/7234848.html 电脑上有了jdk1.7,环境变量配的是1.7,后来再安装了个1.8,结果在cmd-->java ...

  8. 日期 时间选择器(DatePicker和TimePicker)实现用户选择

    日期和时间 作者的设计TimePicker时,大小分布不合理,我调整宽度为match-parent高度为wrap-parent就可以了. public class MainActivity exten ...

  9. SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)

    Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...

  10. [leetcode] 15. Plus One

    这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...