Skip to content

Commit 8ab62d7

Browse files
committed
feat(runtime-core): 支持更新attrs
1 parent 6718589 commit 8ab62d7

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

packages/runtime-core/src/componentProps.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function resolvePorp(propsOptions, rawprops) {
1+
export function resolvePorp(propsOptions, rawprops) {
22
const props = {}
33
const attrs = {}
44
/*
@@ -45,3 +45,50 @@ export function initProps(instance: any, props: any) {
4545
instance.props = newprops || {}
4646
instance.attrs = attrs || {}
4747
}
48+
49+
// 更新props
50+
export function patchComponentProps(props, newProps) {
51+
for (let key in newProps) {
52+
const prop = props[key]
53+
const newProp = newProps[key]
54+
//不同就更新
55+
if (prop !== newProp) {
56+
props[key] = newProps[key]
57+
}
58+
}
59+
// 删除旧的prop不存在与新的prop的属性
60+
/*
61+
newProps:{
62+
name:"coderwei",
63+
}
64+
oldProps:{
65+
name:"coder",
66+
age:19
67+
}
68+
// 这个age在新的component中就不需要了 所以这里可以直接选择删除
69+
*/
70+
for (let key in props) {
71+
if (!(key in newProps)) {
72+
delete props[key]
73+
}
74+
}
75+
}
76+
77+
// 更新组件attrs
78+
export function patchComponentAttrs(attrs, newProps) {
79+
for (const key in attrs) {
80+
const attr = attrs[key]
81+
const newAttr = newProps[key]
82+
if (attr !== newAttr) {
83+
attrs[key] = newProps[key]
84+
}
85+
}
86+
87+
// 删除没用的key
88+
89+
for (const key in attrs) {
90+
if (!(key in newProps)) {
91+
delete attrs[key]
92+
}
93+
}
94+
}

packages/runtime-core/src/renderer.ts

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { effect } from '@coderwei-mini-vue3/reactivity'
2-
import { isKeepAlive, updateSlots } from '@coderwei-mini-vue3/runtime-dom'
2+
import {
3+
isKeepAlive,
4+
patchComponentProps,
5+
updateSlots,
6+
patchComponentAttrs
7+
} from '@coderwei-mini-vue3/runtime-dom'
38
import {
49
EMPTY_OBJECT,
510
invokeArrayFns,
@@ -398,37 +403,10 @@ export function createRenderer(options?) {
398403
}
399404
}
400405

401-
function patchComponentProps(props, newProps) {
402-
for (let key in newProps) {
403-
const prop = props[key]
404-
const newProp = newProps[key]
405-
//不同就更新
406-
if (prop !== newProp) {
407-
props[key] = newProps[key]
408-
}
409-
}
410-
// 删除旧的prop不存在与新的prop的属性
411-
/*
412-
newProps:{
413-
name:"coderwei",
414-
}
415-
oldProps:{
416-
name:"coder",
417-
age:19
418-
}
419-
// 这个age在新的component中就不需要了 所以这里可以直接选择删除
420-
*/
421-
for (let key in props) {
422-
if (!(key in newProps)) {
423-
delete props[key]
424-
}
425-
}
426-
}
427-
428406
function updateComponent(n1: any, n2: any) {
429407
// console.log('更新操作')
430408
const instance = (n2.component = n1.component)
431-
const { props } = instance
409+
const { props, attrs } = instance
432410
const { props: newProps } = n2
433411
/**
434412
* 判断页面内的组件是否需要更新
@@ -439,7 +417,7 @@ export function createRenderer(options?) {
439417
// TODO update prop
440418
patchComponentProps(props, newProps)
441419
// TODO update attrs
442-
420+
patchComponentAttrs(attrs, newProps)
443421
// update slots
444422
updateSlots(instance, n2.children)
445423

0 commit comments

Comments
 (0)