@@ -25,7 +25,7 @@ import {
25
25
asVmProperty ,
26
26
} from './utils/instance'
27
27
import { getVueConstructor } from './runtimeContext'
28
- import { createObserver } from './reactivity/reactive'
28
+ import { createObserver , reactive } from './reactivity/reactive'
29
29
30
30
export function mixin ( Vue : VueConstructor ) {
31
31
Vue . mixin ( {
@@ -192,38 +192,59 @@ export function mixin(Vue: VueConstructor) {
192
192
function createSetupContext (
193
193
vm : ComponentInstance & { [ x : string ] : any }
194
194
) : SetupContext {
195
- const ctx = {
196
- slots : { } ,
197
- } as SetupContext
198
- const props : Array < string | [ string , string ] > = [
195
+ const ctx = { slots : { } } as SetupContext
196
+
197
+ const propsPlain = [
199
198
'root' ,
200
199
'parent' ,
201
200
'refs' ,
202
- 'attrs' ,
203
201
'listeners' ,
204
202
'isServer' ,
205
203
'ssrContext' ,
206
204
]
205
+ const propsReactiveProxy = [ 'attrs' ]
207
206
const methodReturnVoid = [ 'emit' ]
208
- props . forEach ( ( key ) => {
209
- let targetKey : string
210
- let srcKey : string
211
- if ( Array . isArray ( key ) ) {
212
- ; [ targetKey , srcKey ] = key
213
- } else {
214
- targetKey = srcKey = key
215
- }
216
- srcKey = `$${ srcKey } `
217
- proxy ( ctx , targetKey , {
207
+
208
+ propsPlain . forEach ( ( key ) => {
209
+ let srcKey = `$${ key } `
210
+ proxy ( ctx , key , {
218
211
get : ( ) => vm [ srcKey ] ,
219
212
set ( ) {
220
213
warn (
221
- `Cannot assign to '${ targetKey } ' because it is a read-only property` ,
214
+ `Cannot assign to '${ key } ' because it is a read-only property` ,
215
+ vm
216
+ )
217
+ } ,
218
+ } )
219
+ } )
220
+
221
+ propsReactiveProxy . forEach ( ( key ) => {
222
+ let srcKey = `$${ key } `
223
+ proxy ( ctx , key , {
224
+ get : ( ) => {
225
+ const data = reactive ( { } )
226
+ const source = vm [ srcKey ]
227
+
228
+ for ( const attr of Object . keys ( source ) ) {
229
+ proxy ( data , attr , {
230
+ get : ( ) => {
231
+ // to ensure it always return the latest value
232
+ return vm [ srcKey ] [ attr ]
233
+ } ,
234
+ } )
235
+ }
236
+
237
+ return data
238
+ } ,
239
+ set ( ) {
240
+ warn (
241
+ `Cannot assign to '${ key } ' because it is a read-only property` ,
222
242
vm
223
243
)
224
244
} ,
225
245
} )
226
246
} )
247
+
227
248
methodReturnVoid . forEach ( ( key ) => {
228
249
const srcKey = `$${ key } `
229
250
proxy ( ctx , key , {
0 commit comments