5
5
shallowReadonly ,
6
6
proxyRefs ,
7
7
EffectScope ,
8
- markRaw
8
+ markRaw ,
9
+ track ,
10
+ TrackOpTypes
9
11
} from '@vue/reactivity'
10
12
import {
11
13
ComponentPublicInstance ,
@@ -834,19 +836,32 @@ export function finishComponentSetup(
834
836
}
835
837
}
836
838
837
- const attrDevProxyHandlers : ProxyHandler < Data > = {
838
- get : ( target , key : string ) => {
839
- markAttrsAccessed ( )
840
- return target [ key ]
841
- } ,
842
- set : ( ) => {
843
- warn ( `setupContext.attrs is readonly.` )
844
- return false
845
- } ,
846
- deleteProperty : ( ) => {
847
- warn ( `setupContext.attrs is readonly.` )
848
- return false
849
- }
839
+ function createAttrsProxy ( instance : ComponentInternalInstance ) : Data {
840
+ return new Proxy (
841
+ instance . attrs ,
842
+ __DEV__
843
+ ? {
844
+ get ( target , key : string ) {
845
+ markAttrsAccessed ( )
846
+ track ( instance , TrackOpTypes . GET , '$attrs' )
847
+ return target [ key ]
848
+ } ,
849
+ set ( ) {
850
+ warn ( `setupContext.attrs is readonly.` )
851
+ return false
852
+ } ,
853
+ deleteProperty ( ) {
854
+ warn ( `setupContext.attrs is readonly.` )
855
+ return false
856
+ }
857
+ }
858
+ : {
859
+ get ( target , key : string ) {
860
+ track ( instance , TrackOpTypes . GET , '$attrs' )
861
+ return target [ key ]
862
+ }
863
+ }
864
+ )
850
865
}
851
866
852
867
export function createSetupContext (
@@ -859,15 +874,13 @@ export function createSetupContext(
859
874
instance . exposed = exposed || { }
860
875
}
861
876
877
+ let attrs : Data
862
878
if ( __DEV__ ) {
863
- let attrs : Data
864
879
// We use getters in dev in case libs like test-utils overwrite instance
865
880
// properties (overwrites should not be done in prod)
866
881
return Object . freeze ( {
867
882
get attrs ( ) {
868
- return (
869
- attrs || ( attrs = new Proxy ( instance . attrs , attrDevProxyHandlers ) )
870
- )
883
+ return attrs || ( attrs = createAttrsProxy ( instance ) )
871
884
} ,
872
885
get slots ( ) {
873
886
return shallowReadonly ( instance . slots )
@@ -879,7 +892,9 @@ export function createSetupContext(
879
892
} )
880
893
} else {
881
894
return {
882
- attrs : instance . attrs ,
895
+ get attrs ( ) {
896
+ return attrs || ( attrs = createAttrsProxy ( instance ) )
897
+ } ,
883
898
slots : instance . slots ,
884
899
emit : instance . emit ,
885
900
expose
0 commit comments