keep-alive的作用和特点

107 阅读2分钟

u=2788228548,2041937988&fm=253&fmt=auto&app=138&f=JPEG.webp

1.keep-alive
① 是Vue提供的一个抽象组件,用来对组件进行缓存,避免组件重复渲染。简单的说就是不让组件不断的被创建和被销毁;
② 被包裹在keep-alive中的组件状态将会被保留,即你离开是什么状态,回来也是什么状态。
2.activated、deactivated
两个生命函数只有在使用了 keep-alive 组件才可以使用,代表的意义是组件被创建、组件被销毁

  1. include,exclude,max
    include -字符串或正则表达式。只有名称匹配的组件会被缓存。
    exclude - 字符串正则表达式。任何名称匹配的组件都不会被缓存。
    max - 数字。最多可以缓存多少组件实例。

4.目的
可以使被包含的组件保留状态,或避免重新渲染。 一般情况下,在切换组件时,组件会被销毁,为了提高性能可以使用keep-alive来存储;防止组件被销毁;


export default {
  name: 'keep-alive',
  abstract: true, //抽象组件
 
  props: { //接收三个参数
    include: patternTypes,
    exclude: patternTypes,
    max: [String, Number]
  },
 
  created () {
    this.cache = Object.create(null) //缓存的组件
    this.keys = [] //缓存组件的key数组
  },
 
  destroyed () {
    for (const key in this.cache) {
      pruneCacheEntry(this.cache, key, this.keys) //删除缓存中所有组件
    }
  },
 
 /**
    监听include和exclude的值,如果当前cache中的组件不在include中或在exclude中,则
    需要将该组件从cache中去掉。pruneCache方法就是将cache中不满足include和exclude
    规则的组件删除掉
 */
  mounted () {
    this.$watch('include', val => {
      pruneCache(this, name => matches(val, name))
    })
    this.$watch('exclude', val => {
      pruneCache(this, name => !matches(val, name))
    })
  },
 
  render () {
    const slot = this.$slots.default //获取keep-alive标签包裹的默认插槽中的元素
    const vnode: VNode = getFirstComponentChild(slot) //获取到默认插槽中的第一个子元素(keep-alive只对第一个子元素起作用)
    const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
    if (componentOptions) {
      // check pattern
      const name: ?string = getComponentName(componentOptions)
      const { include, exclude } = this
      if ( //如果组件不符合include和exclude规则,那么直接返回该组件,不需要从缓存中获取
        // not included
        (include && (!name || !matches(include, name))) ||
        // excluded
        (exclude && name && matches(exclude, name))
      ) {
        return vnode
      }
 
      const { cache, keys } = this
      const key: ?string = vnode.key == null
        // same constructor may get registered as different local components
        // so cid alone is not enough (#3269)
        ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
        : vnode.key
      if (cache[key]) { //如果缓存中存在当前组件
        vnode.componentInstance = cache[key].componentInstance //将缓存中的组件实例赋给当前组件实例
        // make current key freshest
        remove(keys, key) //将当前组件key从缓存的keys数组中删除
        keys.push(key) //将当前组件keypush到缓存的keys中,以此来保持该组件在缓存中是最新的
      } else { //如果缓存中没有当前组件
        cache[key] = vnode //将当前组件放入缓存中
        keys.push(key) //将当前组件key放入缓存keys数组中
        // prune oldest entry
        if (this.max && keys.length > parseInt(this.max)) { //如果已缓存的组件数量大于max值,则将缓存keys数组中第一个组件删除掉。(缓存中组件的顺序是不常用的在前面,常用的在后面,这是由上面代码中如果组件在缓存中,就需要先在缓存中删除组件key,再重新向缓存keys数组中推入组件key的实现方式决定的)
          pruneCacheEntry(cache, keys[0], keys, this._vnode)
        }
      }
 
      vnode.data.keepAlive = true //标记该组件的keepAlive状态
    }
    return vnode || (slot && slot[0]) //如果上面方法没执行,则直接返回vnode或第一个子元素
  }
}
————————————————
版权声明:本文为CSDN博主「垃圾侠」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiao1215fei/article/details/126241114