run () {
if (this.active) {
const value = this.get()
if (
value !== this.value ||
// Deep watchers and watchers on Object/Arrays should fire even
// when the value is the same, because the value may
// have mutated.
isObject(value) ||
this.deep
) {
// set new value
const oldValue = this.value
this.value = value
if (this.user) {
try {
this.cb.call(this.vm, value, oldValue)
} catch (e) {
handleError(e, this.vm, `callback for watcher "${this.expression}"`)
}
} else {
this.cb.call(this.vm, value, oldValue)
}
}
}
}
执行到 watcher.run 函数中,会先执行 this.get(),即会执行 updateComponent 函数,导致页面重新渲染,之后才进入到 user watcher 的回调函数中,导致无限添加 watcher 到队列中,那么为什么页面没有将 msg 渲染成随机数?而是先报错?
我点击 toggle 后,随机数就显示了