vnode.js 738 Bytes
// 注册函数式组件ref
export function registerRef(vnode, context) {
  if (!context.data.ref) {
    return vnode;
  }
  // 备份vnode原有的insert周期函数
  const hackInsert = vnode.data.hook.insert;
  // 新的vnode的insert周期函数
  vnode.data.hook.insert = function(config) {
    hackInsert(config);
    // 当vnode生成实例后,通过上下文反写入父组件的refs;
    context.parent.$refs[context.data.ref] = config.componentInstance || config.elm; // ref本身就有组件实例和dom节点两种情况,优先取实例
  };
  return vnode;
}

// 简写注册ref
export function ref(name, context) {
  return registerRef(context._c(name, context), context);
}

export default {
  registerRef,
  ref,
};