欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > vue原理分析(十一)研究new Vue()中的initRender

vue原理分析(十一)研究new Vue()中的initRender

2024/11/30 14:36:39 来源:https://blog.csdn.net/qq_29069649/article/details/142125480  浏览:    关键词:vue原理分析(十一)研究new Vue()中的initRender

在Vue.prototype._init 中有一些init函数,今天我们来研究这些init函数

Vue.prototype._init = function (options) {......{initProxy(vm);}......initLifecycle(vm);initEvents(vm);initRender(vm);callHook$1(vm, 'beforeCreate', undefined, false /* setContext */);initInjections(vm); // resolve injections before data/propsinitState(vm);initProvide(vm); // resolve provide after data/propscallHook$1(vm, 'created');......
}

上一篇中已经研究了initEvents,今天我们往下研究

initRender(vm);
function initRender(vm) {vm._vnode = null; // the root of the child treevm._staticTrees = null; // v-once cached treesconst options = vm.$options;const parentVnode = (vm.$vnode = options._parentVnode); // the placeholder node in parent treeconst renderContext = parentVnode && parentVnode.context;vm.$slots = resolveSlots(options._renderChildren, renderContext);vm.$scopedSlots = parentVnode? normalizeScopedSlots(vm.$parent, parentVnode.data.scopedSlots, vm.$slots): emptyObject;// bind the createElement fn to this instance// so that we get proper render context inside it.// args order: tag, data, children, normalizationType, alwaysNormalize// internal version is used by render functions compiled from templates// @ts-expect-errorvm._c = (a, b, c, d) => createElement$1(vm, a, b, c, d, false);// normalization is always applied for the public version, used in// user-written render functions.// @ts-expect-errorvm.$createElement = (a, b, c, d) => createElement$1(vm, a, b, c, d, true);// $attrs & $listeners are exposed for easier HOC creation.// they need to be reactive so that HOCs using them are always updatedconst parentData = parentVnode && parentVnode.data;/* istanbul ignore else */{defineReactive(vm, '$attrs', (parentData && parentData.attrs) || emptyObject, () => {!isUpdatingChildComponent && warn$2(`$attrs is readonly.`, vm);}, true);defineReactive(vm, '$listeners', options._parentListeners || emptyObject, () => {!isUpdatingChildComponent && warn$2(`$listeners is readonly.`, vm);}, true);}
}
function initRender(vm) {vm._vnode = null; // the root of the child treevm._staticTrees = null; // v-once cached treesconst options = vm.$options;const parentVnode = (vm.$vnode = options._parentVnode); // the placeholder node in parent treeconst renderContext = parentVnode && parentVnode.context;......
}

清空 组件的 VNode 对象和静态dom节点树

并获取到该实例的 父组件虚拟dom树对象 parentVnode 与 父组件实例指向 renderContext

function initRender(vm) {......vm.$slots = resolveSlots(options._renderChildren, renderContext);vm.$scopedSlots = parentVnode? normalizeScopedSlots(vm.$parent, parentVnode.data.scopedSlots, vm.$slots): emptyObject;......
}

处理当前组件的 slots 插槽对象 ,以及标准化处理组件的数据域插槽

function resolveSlots(children, context) {// 1.判断是否有children,有没有插槽,如果没有直接返回空对象if (!children || !children.length) {return {};}const slots = {};// for循环遍历子节点for (let i = 0, l = children.length; i < l; i++) {const child = children[i];const data = child.data;// remove slot attribute if the node is resolved as a Vue slot node// 如果节点node已经处理了,移出slot,删除该节点attrs的slotif (data && data.attrs && data.attrs.slot) {delete data.attrs.slot;}// named slots should only be respected if the vnode was rendered in the// same context.// 判断是否是具名插槽,如果是具名插槽,还需要子组件 / 函数子组件 渲染上下文一致// 当需要向子组件的子组件传递具名插槽时,不会保持插槽的名字if ((child.context === context || child.fnContext === context) &&data &&data.slot != null) {const name = data.slot;const slot = slots[name] || (slots[name] = []);//处理父组件采用template形式的插槽if (child.tag === 'template') {slot.push.apply(slot, child.children || []);}else {slot.push(child);}}else {//返回匿名default插槽VNode数组(slots.default || (slots.default = [])).push(child);}}// ignore slots that contains only whitespace// 忽略仅仅包含whitespace的插槽for (const name in slots) {if (slots[name].every(isWhitespace)) {delete slots[name];}}return slots;
}

// 方法用于判断指定字符是否为空白字符,空白符包含:空格、tab键、换行符

function isWhitespace(node) {return (node.isComment && !node.asyncFactory) || node.text === ' ';
}
function initRender(vm) {......// bind the createElement fn to this instance// so that we get proper render context inside it.// args order: tag, data, children, normalizationType, alwaysNormalize// internal version is used by render functions compiled from templates// @ts-expect-errorvm._c = (a, b, c, d) => createElement$1(vm, a, b, c, d, false);// normalization is always applied for the public version, used in// user-written render functions.// @ts-expect-errorvm.$createElement = (a, b, c, d) => createElement$1(vm, a, b, c, d, true);......
}

给 组件 添加两个组件创建方法

_c 表示使用内部 render 函数,不需要额外的标准化处理

$createElement 则表示使用的是用户自己编写的 render 函数,需要内部重新进行一次标准化处理

这两个方法最终其实都是调用的 _createElement 方法,只是标准函数(即 _c)使用 simpleNormalizeChildren() 处理,而用户自定义 render (即 $createElement)使用 normalizeChildren() 处理

function initRender(vm) {......const parentData = parentVnode && parentVnode.data;/* istanbul ignore else */{defineReactive(vm, '$attrs', (parentData && parentData.attrs) || emptyObject, () => {!isUpdatingChildComponent && warn$2(`$attrs is readonly.`, vm);}, true);defineReactive(vm, '$listeners', options._parentListeners || emptyObject, () => {!isUpdatingChildComponent && warn$2(`$listeners is readonly.`, vm);}, true);}......
}

对 $attrs和$listeners  进行响应式处理。


 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com