我正在VueJS中为正在制作的应用程序创建组件,当var更改时,它具有一些监视程序,可将逻辑应用于组件的其他部分。一旦组件被初始化,在用户通过Axios完成一些事件之后,仍然需要服务器上的一些数据来设置它。这些数据从主应用程序发出的事件到达组件。现在的事情是,此var通常会更改(并非总是更改),但是我不希望第一次应用该逻辑,因此我决定设置一个标志并在watcher中检查它是否返回,但是这没有发生:一旦将该标志设置为true(它检查!this.flag),无论如何都会触发观察程序。这是代码:
data(){
return {
isSet: false,
myVar: {
first: 0,
second: 0
}
}
},
watch: {
'myVar.first': {
handler: function(newVal, oldVal){
if(!this.isSet || other.condition){return}
console.log('first triggered');
},
immediate: false
},
'myVar.second': {
handler: function(newVal, oldVal){
if(!this.isSet || other.condition){return}
console.log('second triggered');
},
immediate: false
}
},
methods: {
setBus(){ // gets called on created(){}
let self = this;
Bus.$on('my-event', function(c){
self.doFirstSet(c);
});
},
doFirstSet(c){
// do other setting
if(c.someCondition){
this.doExtraConfig(c);
}
this.isSet = true; // at this point is when …Run Code Online (Sandbox Code Playgroud)