首页 > 代码库 > vue.js 源代码学习笔记 ----- keep-alives
vue.js 源代码学习笔记 ----- keep-alives
/* @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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。