/* @flow */

import { callHook } from 'core/instance/lifecycle'
import { getFirstComponentChild } from 'core/vdom/helpers/index' const patternTypes = [String, RegExp] function matches (pattern: string | RegExp, name: string): boolean {
if (typeof pattern === 'string') {
return pattern.split(',').indexOf(name) > -1
} else {
return pattern.test(name)
}
} export default {
name: 'keep-alive',
abstract: true,
props: {
include: patternTypes,
exclude: patternTypes
},
created () {
this.cache = Object.create(null)
},
render () {
const vnode: VNode = getFirstComponentChild(this.$slots.default)
if (vnode && vnode.componentOptions) {
const opts: VNodeComponentOptions = vnode.componentOptions
// check pattern
const name = opts.Ctor.options.name || opts.tag
if (name && (
(this.include && !matches(this.include, name)) ||
(this.exclude && matches(this.exclude, name))
)) {
return vnode
}
const key = vnode.key == null
// same constructor may get registered as different local components
// so cid alone is not enough (#3269)
? opts.Ctor.cid + (opts.tag ? `::${opts.tag}` : '')
: vnode.key
if (this.cache[key]) {
vnode.child = this.cache[key].child
} else {
this.cache[key] = vnode
}
vnode.data.keepAlive = true
}
return vnode
},
destroyed () {
for (const key in this.cache) {
const vnode = this.cache[key]
callHook(vnode.child, 'deactivated')
vnode.child.$destroy()
}
}
}

vue.js 源代码学习笔记 ----- keep-alives的更多相关文章

  1. vue.js 源代码学习笔记 ----- html-parse.js

    /** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...

  2. vue.js 源代码学习笔记 ----- 工具方法 lang

    /* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...

  3. vue.js 源代码学习笔记 ----- 工具方法 env

    /* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...

  4. vue.js 源代码学习笔记 ----- helpers.js

    /* @flow */ import { parseFilters } from './parser/filter-parser' export function baseWarn (msg: str ...

  5. vue.js 源代码学习笔记 ----- instance render

    /* @flow */ import { warn, nextTick, toNumber, _toString, looseEqual, emptyObject, handleError, loos ...

  6. vue.js 源代码学习笔记 ----- instance event

    /* @flow */ import { updateListeners } from '../vdom/helpers/index' import { toArray, tip, hyphenate ...

  7. vue.js 源代码学习笔记 ----- instance init

    /* @flow */ import config from '../config' import { initProxy } from './proxy' import { initState } ...

  8. vue.js 源代码学习笔记 ----- instance index

    import { initMixin } from './init' import { stateMixin } from './state' import { renderMixin } from ...

  9. vue.js 源代码学习笔记 ----- instance state

    /* @flow */ import Dep from '../observer/dep' import Watcher from '../observer/watcher' import { set ...

  10. vue.js 源代码学习笔记 ----- core lifecycle

    /* @flow */ import config from '../config' import Watcher from '../observer/watcher' import { mark, ...

随机推荐

  1. React Native知识

    http://www.cnblogs.com/wujy/tag/React%20Native/    React Native知识12-与原生交互 React Native知识11-Props(属性) ...

  2. 两个栈实现队列&两个栈实现队列

    为说明思想,假设队列.栈都很大,不会出现满的情况. 1. 两个栈实现队列 //前提已知: struct Stack { int top; //栈顶指针 int stacksize;//栈的大小 int ...

  3. maven-eclipse 中index.html页面乱码

    maven-eclipse 中index.html页面乱码: pox.xml修改: <project> -- <properties> <argLine>-Dfil ...

  4. #C++初学记录(素数判断2)

    素数判断2 比较简单的算法,没有技术含量 A prime number is a natural number which has exactly two distinct natural numbe ...

  5. ASP.NET之报表--RDLC(一)---附源码

    听同事介绍到RDLC,之前有了解过报表,但是确实没什么放在心上.最近有空,就研究下了. 一.RDLC实现 1.步骤 (1)首先新建一个项目RDLCDemo (2)新建一个DataSet数据集,并且绑定 ...

  6. python: 随机选择

    想从一个序列中随机抽取若干元素,或者想生成几个随机数. random 模块有大量的函数用来产生随机数和随机选择元素.比如,要想从一个序列中随机的抽取一个元素,可以使用random.choice() : ...

  7. javascript原生事件总结

    javascript原生的事件,总结了一下,包括事件流.处理函数.事件对象这几样东西.而在兼容性方面,主要是老牌ie8以及以下和现代浏览器的差异,也就是ie和DOM事件标准的差异. 事件流这个事件流i ...

  8. WinterCamp2017 游记

    Winter is coming! Day0 Day0前一天打了一轮CF,做完了ABCD,Div2 Rank59.然后就去开开心心的睡觉,准备第二天的行程. 快到一点的时候躺在了床上,睡不着,翻来覆去 ...

  9. Stitching模块中leaveBiggestComponent初步研究

    在Stitching模块中以及原始论文<Automatic Panoramic Image Stitching using Invariant Features>3.2中,都有" ...

  10. 20145311 《Java程序设计》第八周学习总结

    20145311 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章 NIO与NIO2 高级的输入输出处理,可以使用NIO(New IO),NIO2是文件系统的API 第十五章 ...