Skip to content

Commit 4c4a2ab

Browse files
Kingwlyyx990803
authored andcommitted
trigger event after reassigned state - Fix #5191 (#5233)
1 parent e733e5c commit 4c4a2ab

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Diff for: src/core/observer/scheduler.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ function flushSchedulerQueue () {
6969
}
7070
}
7171

72+
// reset scheduler before updated hook called
73+
const oldQueue = queue.slice()
74+
resetSchedulerState()
75+
7276
// call updated hooks
73-
index = queue.length
77+
index = oldQueue.length
7478
while (index--) {
75-
watcher = queue[index]
79+
watcher = oldQueue[index]
7680
vm = watcher.vm
7781
if (vm._watcher === watcher && vm._isMounted) {
7882
callHook(vm, 'updated')
@@ -84,8 +88,6 @@ function flushSchedulerQueue () {
8488
if (devtools && config.devtools) {
8589
devtools.emit('flush')
8690
}
87-
88-
resetSchedulerState()
8991
}
9092

9193
/**

Diff for: test/unit/modules/observer/scheduler.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,35 @@ describe('Scheduler', () => {
144144
expect(callOrder).toEqual([1, 2, 3])
145145
}).then(done)
146146
})
147+
148+
// Github issue #5191
149+
it('emit should work when updated hook called', done => {
150+
const el = document.createElement('div')
151+
const vm = new Vue({
152+
template: `<div><child @change="bar" :foo="foo"></child></div>`,
153+
data: {
154+
foo: 0
155+
},
156+
methods: {
157+
bar: spy
158+
},
159+
components: {
160+
child: {
161+
template: `<div>{{foo}}</div>`,
162+
props: ['foo'],
163+
updated () {
164+
this.$emit('change')
165+
}
166+
}
167+
}
168+
}).$mount(el)
169+
vm.$nextTick(() => {
170+
vm.foo = 1
171+
vm.$nextTick(() => {
172+
expect(vm.$el.innerHTML).toBe('<div>1</div>')
173+
expect(spy).toHaveBeenCalled()
174+
done()
175+
})
176+
})
177+
})
147178
})

0 commit comments

Comments
 (0)