码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-07-27 18:32:03      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:port   keepalive   local   creat   func   pattern   dex   null   style   

/* @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

标签:port   keepalive   local   creat   func   pattern   dex   null   style   

原文地址:http://www.cnblogs.com/dhsz/p/7245831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!