vue.js 源代码学习笔记 ----- keep-alives

Posted you1you

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

vue.js 源代码学习笔记 ----- decoder

vue.js 源代码学习笔记 ----- Dep

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

vue.js学习笔记八

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

vue.js 源代码学习笔记 ----- keep-alives