Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c2af8df

Browse files
committedMay 5, 2023
feat(runtime-core): 实现挂载组件的时候区分props和attrs
1 parent 3f6e182 commit c2af8df

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed
 

‎packages/runtime-core/src/component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export function createComponentInstance(vnode: any, parentComponent: any) {
3030
provides: parentComponent ? parentComponent.provides : ({} as Record<string, any>), //父组件提供的数据
3131
parent: parentComponent, //父组件实例
3232
isMouted: false, //标志组件是否挂载 后续用于判断是更新还是挂载
33-
subTree: {},
33+
subTree: {}, //子树的虚拟节点
34+
propsOptions: type.props || {}, //组件的props选项,在组件内部会通过defineProps来定义props,因为在使用组件的直接绑定属性不一定全都是props
3435
setupState: {},
3536
next: null,
3637
// 生命周期
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
1+
function resolvePorp(propsOptions, rawprops) {
2+
const props = {}
3+
const attrs = {}
4+
/*
5+
propsOptions{
6+
name:{
7+
type:String,
8+
default:'xxx'
9+
},
10+
age:{
11+
type:Number,
12+
default:xxx
13+
}
14+
}
15+
16+
rawProps:{
17+
name:"coderwei",
18+
age:19,
19+
number: 95527666,
20+
cart: 'bar'
21+
}
22+
*/
23+
const keys = Object.keys(propsOptions)
24+
if (rawprops) {
25+
for (const key in rawprops) {
26+
if (keys.includes(key)) {
27+
props[key] = rawprops[key]
28+
} else {
29+
// 作为attrs
30+
attrs[key] = rawprops[key]
31+
}
32+
}
33+
}
34+
35+
return {
36+
newprops: props,
37+
attrs
38+
}
39+
}
40+
141
// 初始化props
242
export function initProps(instance: any, props: any) {
3-
instance.props = props || {}
43+
// 这里我们需要对props和attrs进行区分 因为我们创建实例的时候,无论是props 还是attrs 都统一传给h函数的第二个参数 h函数的第二个参数会被统一挂载到props
44+
const { newprops, attrs } = resolvePorp(instance.propsOptions, props)
45+
instance.props = newprops || {}
46+
instance.attrs = attrs || {}
447
}

‎packages/runtime-core/src/renderer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ export function createRenderer(options?) {
346346
}
347347

348348
// 处理props
349-
for (const key of Object.getOwnPropertyNames(props)) {
350-
hotPatchProp(el, key, null, props[key])
349+
if (props) {
350+
for (const key of Object.getOwnPropertyNames(props)) {
351+
hotPatchProp(el, key, null, props[key])
352+
}
351353
}
352354
// 将创建的dom元素添加在父元素
353355
hotInsert(el, container, anchor)

‎packages/runtime-core/src/vnode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export { createVNode as createElementBlock }
99
export function createVNode(type, props?, children?) {
1010
const vnode = {
1111
type,
12-
props: props ?? {},
12+
props: props ?? null,
1313
children,
1414
component: null,
1515
key: props && props.key,

0 commit comments

Comments
 (0)
Please sign in to comment.