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
sodatea authored and aJean committed Aug 19, 2020
1 parent f8eff4c commit d80b6f0
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
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
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 d80b6f0

Please sign in to comment.