udpate #2:这不是虫子,是我自己的错.我用extend()
方法扩展了BaseComponent
,并在选项中加入了extends: BaseComponent
.我以为两者都需要,但extends: BaseComponent
个选项似乎就足够了
update:我发现了这个问题的原因:观察者似乎是重复的,因为它在BaseComponent
中,由我的例子中使用的Component
扩展.
使用vue-cli
和单文件组件
<template>
<div>
<other-component :my-object="myObject" />
</div>
</template>
<script>
export default (Vue as VueConstructor).extend({
data() {
return {
myObject: null
}
},
methods: {
actionButtonClicked(action, ID) {
console.log('actionButtonClicked');
this.myObject = {
action: action,
ID: ID
}
}
}
});
</script>
然后我用一个观察者观察另一个组件中的props ——但是每次执行该方法时,watch都会被调用两次.
<script>
export default (Vue as VueConstructor<Vue>).extend({
/* ... */
props: {
myObject: {
type: Object,
default: null
},
watch: {
'myObject.ID'(value, oldValue) {
console.log('watcher executed');
}
}
/* ... */
});
</script>
所以在控制台中,我得到了输出:
actionButtonClicked
watcher executed
watcher executed
…每次调用该方法时.
我已经try 过所有不同的观察者——例如deep: true
+handler
.但这一切并没有改变观察者被呼叫两次的情况.