• 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()
        }
      }
    }
  • 相关阅读:
    文件上传和多线程通信
    黏包
    socket通信
    osi七层协议
    面向对象的反射和双下方法
    类的成员和异常处理
    python面向对象类的约束和设计的统一化规范
    单继承和多继承
    对象
    Python-----带参数的装饰器以及补充
  • 原文地址:https://www.cnblogs.com/dhsz/p/7245831.html
Copyright © 2020-2023  润新知