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 (vuejs#8381)

fix vuejs#8076
  • Loading branch information
haoqunjiang authored and hefeng committed Jan 25, 2019
1 parent e2abd1b commit 5665758
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 5665758

Please sign in to comment.