Skip to content

Commit

Permalink
fix(lifecycle): updated should not be called after component being de…
Browse files Browse the repository at this point in the history
…stroyed (#8381)

fix #8076
  • Loading branch information
haoqunjiang authored and yyx990803 committed Oct 24, 2018
1 parent a71853b commit a64ff19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/observer/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function callUpdatedHooks (queue) {
while (i--) {
const watcher = queue[i]
const vm = watcher.vm
if (vm._watcher === watcher && vm._isMounted) {
if (vm._watcher === watcher && vm._isMounted && !vm._isDestroyed) {
callHook(vm, 'updated')
}
}
Expand Down
37 changes: 37 additions & 0 deletions test/unit/features/options/lifecycle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,43 @@ describe('Options lifecycle hooks', () => {
expect(calls).toEqual(['child', 'parent'])
}).then(done)
})

// #8076
it('should not be called after destroy', done => {
const updated = jasmine.createSpy('updated')
const destroyed = jasmine.createSpy('destroyed')

Vue.component('todo', {
template: '<div>{{todo.done}}</div>',
props: ['todo'],
destroyed,
updated
})

const vm = new Vue({
template: `
<div>
<todo v-for="t in pendingTodos" :todo="t" :key="t.id"></todo>
</div>
`,
data () {
return {
todos: [{ id: 1, done: false }]
}
},
computed: {
pendingTodos () {
return this.todos.filter(t => !t.done)
}
}
}).$mount()

vm.todos[0].done = true
waitForUpdate(() => {
expect(destroyed).toHaveBeenCalled()
expect(updated).not.toHaveBeenCalled()
}).then(done)
})
})

describe('beforeDestroy', () => {
Expand Down

0 comments on commit a64ff19

Please sign in to comment.