@@ -7,6 +7,7 @@ function createVNode(type, props, children) {
77 type,
88 props,
99 children,
10+ el : null ,
1011 } ;
1112 return vnode ;
1213}
@@ -21,6 +22,20 @@ function isString(value) {
2122 return Object . prototype . toString . call ( value ) === "[object String]" ;
2223}
2324
25+ const PublicPropertiesMaps = {
26+ "$el" : ( instance ) => instance . vnode . el ,
27+ } ;
28+ const ComponentPublicInstanceHandlers = {
29+ get ( { _instance } , key ) {
30+ if ( key in _instance . setupState ) {
31+ return _instance . setupState [ key ] ;
32+ }
33+ if ( key in PublicPropertiesMaps ) {
34+ return PublicPropertiesMaps [ key ] ( _instance ) ;
35+ }
36+ }
37+ } ;
38+
2439function createComponentInstance ( vnode ) {
2540 const component = {
2641 vnode,
@@ -40,14 +55,8 @@ function initProps(instance) {
4055}
4156function setupStatefulComponent ( instance ) {
4257 const Component = instance . type ;
43- const context = { } ;
44- instance . proxy = new Proxy ( context , {
45- get ( target , key ) {
46- if ( key in instance . setupState ) {
47- return instance . setupState [ key ] ;
48- }
49- }
50- } ) ;
58+ const context = { _instance : instance , _component : Component } ;
59+ instance . proxy = new Proxy ( context , ComponentPublicInstanceHandlers ) ;
5160 if ( Component . setup ) {
5261 const setupResult = Component . setup ( ) ;
5362 handleSetupResult ( instance , setupResult ) ;
@@ -87,11 +96,12 @@ function processComponent(vnode, container) {
8796function mountComponent ( vnode , container ) {
8897 const instance = createComponentInstance ( vnode ) ;
8998 setupComponent ( instance ) ;
90- setupRenderEffect ( instance , container ) ;
99+ setupRenderEffect ( instance , container , vnode ) ;
91100}
92101function mountElement ( vnode , container ) {
93102 // vnode type -> div/span
94- const el = document . createElement ( vnode . type ) ;
103+ // vnode.el -> element.el
104+ const el = ( vnode . el = document . createElement ( vnode . type ) ) ;
95105 // children
96106 if ( isString ( vnode . children ) ) {
97107 el . textContent = vnode . children ;
@@ -116,9 +126,10 @@ function addAttrs(vnode, container) {
116126 }
117127 }
118128}
119- function setupRenderEffect ( instance , container ) {
129+ function setupRenderEffect ( instance , container , vnode ) {
120130 const subTree = instance . render . call ( instance . proxy ) ;
121131 patch ( subTree , container ) ;
132+ vnode . el = subTree . el ;
122133}
123134
124135function createApp ( rootComponent ) {
0 commit comments