Skip to content

Commit 78d1821

Browse files
dssengyyx990803
authored andcommitted
refactor(componentProxy): simplify proxy handling (#214)
1 parent 5d98c51 commit 78d1821

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

packages/runtime-core/src/componentProxy.ts

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ export type ComponentPublicInstance<
3636
ExtractComputedReturns<C> &
3737
M
3838

39+
const publicPropertiesMap = {
40+
$data: 'data',
41+
$props: 'propsProxy',
42+
$attrs: 'attrs',
43+
$slots: 'slots',
44+
$refs: 'refs',
45+
$parent: 'parent',
46+
$root: 'root',
47+
$emit: 'emit',
48+
$options: 'type'
49+
}
50+
3951
export const PublicInstanceProxyHandlers = {
4052
get(target: ComponentInternalInstance, key: string) {
4153
const { renderContext, data, props, propsProxy } = target
@@ -46,44 +58,23 @@ export const PublicInstanceProxyHandlers = {
4658
} else if (hasOwn(props, key)) {
4759
// return the value from propsProxy for ref unwrapping and readonly
4860
return propsProxy![key]
49-
} else {
50-
// TODO simplify this?
61+
} else if (key === '$el') {
62+
return target.vnode.el
63+
} else if (hasOwn(publicPropertiesMap, key)) {
64+
return target[publicPropertiesMap[key]]
65+
}
66+
// methods are only exposed when options are supported
67+
if (__FEATURE_OPTIONS__) {
5168
switch (key) {
52-
case '$data':
53-
return data
54-
case '$props':
55-
return propsProxy
56-
case '$attrs':
57-
return target.attrs
58-
case '$slots':
59-
return target.slots
60-
case '$refs':
61-
return target.refs
62-
case '$parent':
63-
return target.parent
64-
case '$root':
65-
return target.root
66-
case '$emit':
67-
return target.emit
68-
case '$el':
69-
return target.vnode.el
70-
case '$options':
71-
return target.type
72-
default:
73-
// methods are only exposed when options are supported
74-
if (__FEATURE_OPTIONS__) {
75-
switch (key) {
76-
case '$forceUpdate':
77-
return target.update
78-
case '$nextTick':
79-
return nextTick
80-
case '$watch':
81-
return instanceWatch.bind(target)
82-
}
83-
}
84-
return target.user[key]
69+
case '$forceUpdate':
70+
return target.update
71+
case '$nextTick':
72+
return nextTick
73+
case '$watch':
74+
return instanceWatch.bind(target)
8575
}
8676
}
77+
return target.user[key]
8778
},
8879
// this trap is only called in browser-compiled render functions that use
8980
// `with (this) {}`

0 commit comments

Comments
 (0)