前言
前面我們簡單的了解了 vue 初始化時的一些大概的流程,這里我們詳細的了解下具體的內容;
內容
這一塊主要圍繞init.ts中的initRender進行剖析,引數合并完成之后就開始了初始化生命周期,
initRender
initRender位于src/core/instance/render.ts
export function initRender(vm: Component) {
vm._vnode = null // the root of the child tree
vm._staticTrees = null // v-once cached trees
const options = vm.$options
const parentVnode = (vm.$vnode = options._parentVnode!) // the placeholder node in parent tree
const renderContext = parentVnode && (parentVnode.context as Component)
// 插槽
// https://v2.cn.vuejs.org/v2/api/#vm-slots
vm.$slots = resolveSlots(options._renderChildren, renderContext)
// 作用域插槽
// https://v2.cn.vuejs.org/v2/api/#vm-scopedSlots
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-error
// 將createElement函式系結到實體上,以保證正確的背景關系渲染順序,
// 內部版本使用的渲染函式來自模板編譯
vm._c = (a, b, c, d) => createElement(vm, a, b, c, d, false)
// normalization is always applied for the public version, used in
// user-written render functions.
// 公共版本使用用戶撰寫的渲染函式
// @ts-expect-error
vm.$createElement = (a, b, c, d) => createElement(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 updated
const parentData = https://www.cnblogs.com/wangyang0210/archive/2023/03/23/parentVnode && parentVnode.data
/* istanbul ignore else */
if (__DEV__) {
defineReactive(
vm,'$attrs',
(parentData && parentData.attrs) || emptyObject,
() => {
!isUpdatingChildComponent && warn(`$attrs is readonly.`, vm)
},
true
)
defineReactive(
vm,
'$listeners',
options._parentListeners || emptyObject,
() => {
!isUpdatingChildComponent && warn(`$listeners is readonly.`, vm)
},
true
)
} else {
// 通過defineReactive將$attrs和$listeners設定為回應式資料 | 這一塊后面再詳說
// emptyObject 回傳一個被凍結的物件空物件,不能被修改其原型也不能被修改
// 包含了父作用域中不作為 prop 被識別 (且獲取) 的 attribute 系結 (class 和 style 除外),
// 當一個組件沒有宣告任何 prop 時,這里會包含所有父作用域的系結(class 和 style 除外),
// 并且可以通過 v-bind="$attrs" 傳入內部組件——在創建高級別的組件時非常有用,
// https://v2.cn.vuejs.org/v2/api/?#vm-attrs
defineReactive(
vm,
'$attrs',
(parentData && parentData.attrs) || emptyObject,
null,
true
)
// 包含了父作用域中的 (不含 .native 修飾器的) v-on 事件監聽器,
// 它可以通過 v-on="$listeners" 傳入內部組件——在創建更高層次的組件時非常有用,
// https://v2.cn.vuejs.org/v2/api/?#vm-listeners
defineReactive(
vm,
'$listeners',
options._parentListeners || emptyObject,
null,
true
)
}
}
resolveSlots
resolveSlots位于src/core/instance/render-helpers/resolve-slots.ts
**
* Runtime helper for resolving raw children VNodes into a slot object.
*
* 將children VNodes 轉化為 slot 物件
*/
export function resolveSlots(
children: Array<VNode> | null | undefined,
context: Component | null
): { [key: string]: Array<VNode> } {
// 如果不存在子節點直接回傳空物件
if (!children || !children.length) {
return {}
}
// 定義一個slots空物件,存放slot
const slots: Record<string, any> = {}
// 對子節點進行遍歷
for (let i = 0, l = children.length; i < l; i++) {
const child = children[i]
const data = https://www.cnblogs.com/wangyang0210/archive/2023/03/23/child.data
// remove slot attribute if the node is resolved as a Vue slot node
// 如果節點是slot節點的話就移除slot的屬性
if (data && data.attrs && data.attrs.slot) {
// 洗掉該節點attrs的slot
delete data.attrs.slot
}
// named slots should only be respected if the vnode was rendered in the
// same context.
// 判斷是否為具名插槽
// https://v2.cn.vuejs.org/v2/guide/components-slots.html#%E5%85%B7%E5%90%8D%E6%8F%92%E6%A7%BD
if (
(child.context === context || child.fnContext === context) &&
data &&
data.slot != null
) {
// 獲取插槽的名字
const name = data.slot
// 插槽名字不存在則賦值為空陣列
const slot = slots[name] || (slots[name] = [])
// 如果是template元素將child.children添加到陣列中
if (child.tag ==='template') {
slot.push.apply(slot, child.children || [])
} else {
slot.push(child)
}
} else {
// 回傳匿名的slot,名字是default
;(slots.default || (slots.default = [])).push(child)
}
}
// ignore slots that contains only whitespace
// 忽略空白內容的插槽
for (const name in slots) {
// 利用every檢測指定的slots陣列下的所有結果是否能夠通過isWhitespace的測驗
// 全部通過才會回傳true
if (slots[name].every(isWhitespace)) {
delete slots[name]
}
}
return slots
}
function isWhitespace(node: VNode): boolean {
return (node.isComment && !node.asyncFactory) || node.text === ' '
}
學無止境,謙卑而行.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/547839.html
標籤:其他
