keep-alive 组件

118 阅读4分钟

<KeepAlive> 是一个内置组件,它的功能是在多个组件间动态切换时缓存被移除的组件实例。

先粗略介绍一下<KeepAlive>

  • 是一个 Vue 全局组件(抽象组件)。
  • 本身不会渲染出来,也不会出现在父组件链中。
  • 包裹动态组件时,会缓存不活动的组件,而不是销毁它们。

接收三个参数:

  1. include:可传字符串、正则表达式、数组,名称匹配成功的组件会被缓存
  2. exclude:可传字符串、正则表达式、数组,名称匹配成功的组件不会被缓存
  3. max:可传数字,限制缓存组件的最大数量

用法也很简单

<!-- 动态组件 -->
<keep-alive :include="whiteList" :exclude="blackList" :max="amount">
  <component :is="currentComponent"></component>
</keep-alive>
<!-- vue-router -->
<keep-alive :include="whiteList" :exclude="blackList" :max="amount">
  <router-view></router-view>
</keep-alive>

开始初探

// src/core/components/keep-alive.js
export default {
  name: "keep-alive",
  abstract: true, // 判断此组件是否需要在渲染成真实DOM
  props: {
    include: patternTypes,
    exclude: patternTypes,
    max: [String, Number],
  },
  created() {
    this.cache = Object.create(null); // 创建对象来存储  缓存虚拟dom
    this.keys = []; // 创建数组来存储  缓存key
  },
  mounted() {
    // 实时监听include、exclude的变动
    this.$watch("include", (val) => {
      pruneCache(this, (name) => matches(val, name));
    });
    this.$watch("exclude", (val) => {
      pruneCache(this, (name) => !matches(val, name));
    });
  },
  destroyed() {
    for (const key in this.cache) {
      // 删除所有的缓存
      pruneCacheEntry(this.cache, key, this.keys);
    }
  },
  render() {
    // 下面讲
  },
};

可以看出,与我们定义组件的过程一样,先是设置组件名为 keep-alive,其次定义了一个 abstract 属性,值为 true。

这个属性在 vue 的官方教程并未提及,却至关重要,后面的渲染过程会用到。props 属性定义了 keep-alive 组件支持的全部参数

keep-alive 在它生命周期内定义了三个钩子函数:

  • created: 初始化两个对象分别缓存 VNode(虚拟 DOM)和 VNode 对应的键集合。
  • destroyed: 删除 this.cache 中缓存的 VNode 实例。我们留意到,这里不是简单地将 this.cache 置为 null,而是遍历调用 pruneCacheEntry 函数删除。

pruneCacheEntry 函数

// src/core/components/keep-alive.js
function pruneCacheEntry(
  cache: VNodeCache,
  key: string,
  keys: Array<string>,
  current?: VNode
) {
  const cached = cache[key];
  if (cached && (!current || cached.tag !== current.tag)) {
    cached.componentInstance.$destroy(); // 执行组件的 destroy 钩子函数
  }
  cache[key] = null;
  remove(keys, key);
}

总结一下就是做了三件事:

  1. 遍历集合,执行所有缓存组件的$destroy 方法
  2. 将 cache 对应 key 的内容设置为 null
  3. 删除 keys 中对应的元素

pruneCache 函数的核心也是去调用 pruneCacheEntry。

function pruneCache(keepAliveInstance: any, filter: Function) {
  const { cache, keys, _vnode } = keepAliveInstance;
  for (const key in cache) {
    const cachedNode: ?VNode = cache[key];
    if (cachedNode) {
      const name: ?string = getComponentName(cachedNode.componentOptions);
      if (name && !filter(name)) {
        pruneCacheEntry(cache, key, keys, _vnode);
      }
    }
  }
}

render 干了啥?

  // src/core/components/keep-alive.js
  render () {
    const slot = this.$slots.default
    const vnode: VNode = getFirstComponentChild(slot) // 找到第一个子组件对象
    const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions
    if (componentOptions) { // 存在组件参数
      // check pattern
      const name: ?string = getComponentName(componentOptions) // 组件名
      const { include, exclude } = this
      if ( // 条件匹配
        // 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 // 定义组件的缓存key
        // 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)
        keys.push(key) // 调整key排序
      } else {
        cache[key] = vnode // 缓存组件对象
        keys.push(key)
        // prune oldest entry
        if (this.max && keys.length > parseInt(this.max)) { // 超过缓存数限制,将第一个删除
          pruneCacheEntry(cache, keys[0], keys, this._vnode)
        }
      }

      vnode.data.keepAlive = true // 渲染和执行被包裹组件的钩子函数需要用到
    }
    return vnode || (slot && slot[0])
  }

  1. 获取 keep-alive 包裹着的第一个子组件对象及其组件名;
  2. 根据设定的黑白名单(如果有)进行条件匹配,决定是否缓存。不匹配,直接返回组件实例(VNode),否则执行第三步;
  3. 根据组件 ID 和 tag 生成缓存 Key,并在缓存对象中查找是否已缓存过该组件实例。 如果存在,直接取出缓存值并更新该 key 在 this.keys 中的位置(更新 key 的位置是实现 LRU 置换策略的关键),否则执行第四步;
  4. 在 this.cache 对象中存储该组件实例并保存 key 值,之后检查缓存的实例数量是否超过 max 的设置值,超过则根据 LRU 置换策略删除最近最久未使用的实例(即是下标为 0 的那个 key)。
  5. 最后并且很重要,将该组件实例的 keepAlive 属性值设置为 true。

渲染

Vue 的渲染过程

Vue 的渲染过程

Vue 的渲染是从图中的 render 阶段开始的,但 keep-alive 的渲染是在 patch 阶段,这是构建组件树(虚拟 DOM 树),并将 VNode 转换成真正 DOM 节点的过程。

简单描述过程

import App from "./App.vue";
new Vue({
  render: (h) => h(App),
}).$mount("#app");
  • Vue 在渲染的时候先调用原型上的_render函数将组件对象转化为一个 VNode 实例; 而_render是通过调用 createElementcreateEmptyVNode 两个函数进行转化;
  • createElement: 转化过程会根据不同的情形选择 new VNode 或者调用 createComponent 函数做 VNode 实例化;
  • 完成 VNode 实例化后,这时候 Vue 调用原型上的_update函数把 VNode 渲染为真实 DOM,这个过程又是通过调用patch函数完成的(这就是 pacth 阶段了)

Vue 的渲染过程

keep-alive 的渲染

// src/core/instance/lifecycle.js
export function initLifecycle(vm: Component) {
  const options = vm.$options;
  // 找到第一个非 abstract 的父组件实例
  let parent = options.parent;
  if (parent && !options.abstract) {
    while (parent.$options.abstract && parent.$parent) {
      parent = parent.$parent;
    }
    parent.$children.push(vm);
  }
  vm.$parent = parent;
  // ...
}

Vue 在初始化生命周期的时候,为组件实例建立父子关系会根据 abstract 属性决定是否忽略某个组件。 在 keep-alive 中,设置了 abstract: true,那 Vue 就会跳过该组件实例

最后构建的组件树中就不会包含 keep-alive 组件,那么由组件树渲染成的 DOM 树自然也不会有 keep-alive 相关的节点了。

包裹的组件是如何使用缓存的?

在 patch 阶段,会执行 createComponent 函数:

// src/core/vdom/patch.js
function createComponent(vnode, insertedVnodeQueue, parentElm, refElm) {
  let i = vnode.data;
  if (isDef(i)) {
    const isReactivated = isDef(vnode.componentInstance) && i.keepAlive;
    if (isDef((i = i.hook)) && isDef((i = i.init))) {
      i(vnode, false /* hydrating */);
    }
    if (isDef(vnode.componentInstance)) {
      initComponent(vnode, insertedVnodeQueue);
      insert(parentElm, vnode.elm, refElm); // 将缓存的DOM(vnode.elm)插入父元素中
      if (isTrue(isReactivated)) {
        reactivateComponent(vnode, insertedVnodeQueue, parentElm, refElm);
      }
      return true;
    }
  }
}
  • 在首次加载被包裹组件时,由 keep-alive.js 中的 render 函数可知, vnode.componentInstance 的值是 undefined,keepAlive 的值是 true, 因为 keep-alive 组件作为父组件,它的 render 函数会先于被包裹组件执行; 那么就只执行到 i(vnode, false /* hydrating */),后面的逻辑不再执行;

  • 再次访问被包裹组件时,vnode.componentInstance 的值就是已经缓存的组件实例, 那么会执行insert(parentElm, vnode.elm, refElm)逻辑,这样就直接把上一次的 DOM 插入到了父元素中。

钩子函数

一般的组件,每一次加载都会有完整的生命周期,即生命周期里面对应的钩子函数都会被触发,为什么被 keep-alive 包裹的组件却不是呢? 我们在前面章节分析到,被缓存的组件实例会为其设置 keepAlive = true,而在初始化组件钩子函数中:

// src/core/vdom/create-component.js
const componentVNodeHooks = {
  init(vnode: VNodeWithData, hydrating: boolean): ?boolean {
    if (
      vnode.componentInstance &&
      !vnode.componentInstance._isDestroyed &&
      vnode.data.keepAlive
    ) {
      // kept-alive components, treat as a patch
      const mountedNode: any = vnode; // work around flow
      componentVNodeHooks.prepatch(mountedNode, mountedNode);
    } else {
      const child = (vnode.componentInstance = createComponentInstanceForVnode(
        vnode,
        activeInstance
      ));
      child.$mount(hydrating ? vnode.elm : undefined, hydrating);
    }
  },
  // ...
};

可以看出,当 vnode.componentInstance 和 keepAlive 同时为 true 值时,不再进入$mount 过程,那 mounted 之前的所有钩子函数(beforeCreate、created、mounted)都不再执行。

可重复的 activated

在 patch 的阶段,最后会执行 invokeInsertHook 函数,而这个函数就是去调用组件实例(VNode)自身的 insert 钩子:

// src/core/vdom/patch.js
function invokeInsertHook(vnode, queue, initial) {
  if (isTrue(initial) && isDef(vnode.parent)) {
    vnode.parent.data.pendingInsert = queue;
  } else {
    for (let i = 0; i < queue.length; ++i) {
      queue[i].data.hook.insert(queue[i]); // 调用VNode自身的insert钩子函数
    }
  }
}
// src/core/vdom/create-component.js
const componentVNodeHooks = {
  // init()
  insert(vnode: MountedComponentVNode) {
    const { context, componentInstance } = vnode;
    if (!componentInstance._isMounted) {
      componentInstance._isMounted = true;
      callHook(componentInstance, "mounted");
    }
    if (vnode.data.keepAlive) {
      if (context._isMounted) {
        queueActivatedComponent(componentInstance);
      } else {
        activateChildComponent(componentInstance, true /* direct */);
      }
    }
    // ...
  },
};

在这个钩子里面,调用了 activateChildComponent 函数递归地去执行所有子组件的 activated 钩子函数:

// src/core/instance/lifecycle.js
export function activateChildComponent(vm: Component, direct?: boolean) {
  if (direct) {
    vm._directInactive = false;
    if (isInInactiveTree(vm)) {
      return;
    }
  } else if (vm._directInactive) {
    return;
  }
  if (vm._inactive || vm._inactive === null) {
    vm._inactive = false;
    for (let i = 0; i < vm.$children.length; i++) {
      activateChildComponent(vm.$children[i]);
    }
    callHook(vm, "activated");
  }
}

相反地,deactivated 钩子函数也是一样的原理,在组件实例(VNode)的 destroy 钩子函数中调用 deactivateChildComponent 函数。

总结

keep-alive 主要是利用了 Vue.js 的抽象组件和生命周期钩子函数。当一个组件被包裹在 <keep-alive> 中时,它实际上会被作为一个抽象组件,不会直接渲染到页面上。 当组件第一次渲染时,created 和 mounted 生命周期钩子函数会被调用。 而当组件被包裹在 keep-alive 中时,它将多出两个生命周期钩子函数:activated 和 deactivated。

  • activated 钩子函数会在组件被激活时调用,即切换到包含该组件的页面时。
  • deactivated 钩子函数会在组件被停用时调用,即切换到不包含该组件的页面时。

keep-alive 通过监听路由变化来判断何时激活或停用组件。当切换到包含被缓存组件的页面时,activated 钩子函数会被调用,此时组件的状态得以保留。 而当切换到不包含被缓存组件的页面时,deactivated 钩子函数会被调用,组件被停用,不再占用内存。