File tree 2 files changed +33
-2
lines changed
test/unit/features/v3/reactivity
2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { updateComponentListeners } from './events'
6
6
import { resolveSlots } from './render-helpers/resolve-slots'
7
7
import { toggleObserving } from '../observer/index'
8
8
import { pushTarget , popTarget } from '../observer/dep'
9
+ import { getCurrentScope } from '../../v3/reactivity/effectScope'
9
10
import type { Component } from 'types/component'
10
11
import type { MountedComponentVNode } from 'types/vnode'
11
12
@@ -398,7 +399,8 @@ export function callHook(
398
399
) {
399
400
// #7573 disable dep collection when invoking lifecycle hooks
400
401
pushTarget ( )
401
- const prev = currentInstance
402
+ const prevInst = currentInstance
403
+ const prevScope = getCurrentScope ( )
402
404
setContext && setCurrentInstance ( vm )
403
405
const handlers = vm . $options [ hook ]
404
406
const info = `${ hook } hook`
@@ -410,6 +412,10 @@ export function callHook(
410
412
if ( vm . _hasHookEvent ) {
411
413
vm . $emit ( 'hook:' + hook )
412
414
}
413
- setContext && setCurrentInstance ( prev )
415
+ if ( setContext ) {
416
+ setCurrentInstance ( prevInst )
417
+ prevScope ?. on ( )
418
+ }
419
+
414
420
popTarget ( )
415
421
}
Original file line number Diff line number Diff line change
1
+ import Vue from 'vue'
1
2
import { nextTick } from 'core/util'
2
3
import {
3
4
watch ,
@@ -290,4 +291,28 @@ describe('reactivity/effectScope', () => {
290
291
expect ( getCurrentScope ( ) ) . toBe ( parentScope )
291
292
} )
292
293
} )
294
+
295
+ it ( 'test scope should not break currentScope when component call hooks' , ( ) => {
296
+ const scope = new EffectScope ( )
297
+ const vm = new Vue ( {
298
+ template : `
299
+ <div>
300
+ <div v-if="show" />
301
+ </div>
302
+ ` ,
303
+ data ( ) {
304
+ return {
305
+ show : false
306
+ }
307
+ }
308
+ } ) . $mount ( )
309
+
310
+ scope . run ( ( ) => {
311
+ // call renderTriggered hook here
312
+ vm . show = true
313
+ // this effect should be collected by scope not the component scope
314
+ effect ( ( ) => { } )
315
+ } )
316
+ expect ( scope . effects . length ) . toBe ( 1 )
317
+ } )
293
318
} )
You can’t perform that action at this time.
0 commit comments