zoukankan      html  css  js  c++  java
  • 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()
        }
      }
    }
  • 相关阅读:
    BUAA 软工 | 从计算机技术中探索艺术之路
    好好编程BUAA_SE(组/团队) Scrum Meeting 博客汇总
    beta事后分析
    Beta阶段项目展示
    Beta阶段测试报告
    Beta版本发布计划
    Beta阶段 第十次Scrum Meeting
    Beta阶段 第九次Scrum Meeting
    Beta阶段 第八次Scrum Meeting
    Beta阶段 第七次Scrum Meeting
  • 原文地址:https://www.cnblogs.com/dhsz/p/7245831.html
Copyright © 2011-2022 走看看