@@ -5,10 +5,12 @@ import {
55 getCurrentInstance ,
66 ComponentInternalInstance ,
77 InternalSlots ,
8+ SetupContext ,
89} from '../runtimeContext'
910import { Ref , isRef , isReactive } from '../apis'
1011import { hasOwn , proxy , warn } from './utils'
1112import { createSlotProxy , resolveSlots } from './helper'
13+ import { reactive } from '../reactivity/reactive'
1214
1315export function asVmProperty (
1416 vm : ComponentInstance ,
@@ -101,6 +103,42 @@ export function updateTemplateRef(vm: ComponentInstance) {
101103 vmStateManager . set ( vm , 'refs' , validNewKeys )
102104}
103105
106+ export function updateVmAttrs ( vm : ComponentInstance , ctx : SetupContext ) {
107+ if ( ! vm || ! ctx ) {
108+ return
109+ }
110+ let attrBindings = vmStateManager . get ( vm , 'attrBindings' )
111+ if ( ! attrBindings ) {
112+ const observedData = reactive ( { } )
113+ vmStateManager . set ( vm , 'attrBindings' , observedData )
114+ attrBindings = observedData
115+ proxy ( ctx , 'attrs' , {
116+ get : ( ) => {
117+ return attrBindings
118+ } ,
119+ set ( ) {
120+ __DEV__ &&
121+ warn (
122+ `Cannot assign to '$attrs' because it is a read-only property` ,
123+ vm
124+ )
125+ } ,
126+ } )
127+ }
128+
129+ const source = vm . $attrs
130+ for ( const attr of Object . keys ( source ) ) {
131+ if ( ! hasOwn ( attrBindings ! , attr ) ) {
132+ proxy ( attrBindings , attr , {
133+ get : ( ) => {
134+ // to ensure it always return the latest value
135+ return vm . $attrs [ attr ]
136+ } ,
137+ } )
138+ }
139+ }
140+ }
141+
104142export function resolveScopedSlots (
105143 vm : ComponentInstance ,
106144 slotsProxy : InternalSlots
0 commit comments