Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/vuePublic

Commit6d857f5

Browse files
committed
fix(watch): new property addition should trigger deep watcher with getter
close#12967close#12972
1 parenta174c29 commit6d857f5

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

‎src/v3/apiWatch.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ function doWatch(
185185
}
186186

187187
constinstance=currentInstance
188-
constcall=(fn:Function,type:string,args:any[]|null=null)=>
189-
invokeWithErrorHandling(fn,null,args,instance,type)
188+
constcall=(fn:Function,type:string,args:any[]|null=null)=>{
189+
constres=invokeWithErrorHandling(fn,null,args,instance,type)
190+
if(deep&&res&&res.__ob__)res.__ob__.dep.depend()
191+
returnres
192+
}
190193

191194
letgetter:()=>any
192195
letforceTrigger=false
@@ -209,6 +212,7 @@ function doWatch(
209212
if(isRef(s)){
210213
returns.value
211214
}elseif(isReactive(s)){
215+
s.__ob__.dep.depend()
212216
returntraverse(s)
213217
}elseif(isFunction(s)){
214218
returncall(s,WATCHER_GETTER)

‎test/unit/features/v3/apiWatch.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,4 +1200,35 @@ describe('api: watch', () => {
12001200
expect(parentSpy).toHaveBeenCalledTimes(1)
12011201
expect(childSpy).toHaveBeenCalledTimes(1)
12021202
})
1203+
1204+
// #12967
1205+
test('trigger when adding new property with Vue.set (getter)',async()=>{
1206+
constspy=vi.fn()
1207+
constr=reactive({exist:5})
1208+
watch(()=>r,spy,{deep:true})
1209+
set(r,'add',1)
1210+
1211+
awaitnextTick()
1212+
expect(spy).toHaveBeenCalledTimes(1)
1213+
})
1214+
1215+
test('trigger when adding new property with Vue.set (getter in array source)',async()=>{
1216+
constspy=vi.fn()
1217+
constr=reactive({exist:5})
1218+
watch([()=>r],spy,{deep:true})
1219+
set(r,'add',1)
1220+
1221+
awaitnextTick()
1222+
expect(spy).toHaveBeenCalledTimes(1)
1223+
})
1224+
1225+
test('trigger when adding new property with Vue.set (reactive in array source)',async()=>{
1226+
constspy=vi.fn()
1227+
constr=reactive({exist:5})
1228+
watch([r],spy,{deep:true})
1229+
set(r,'add',1)
1230+
1231+
awaitnextTick()
1232+
expect(spy).toHaveBeenCalledTimes(1)
1233+
})
12031234
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp