调用obtainAList()函数时不希望触发watch: { selectors: debounce(function () { this.calendarComponentUp(); console.log(this.timeArray); this.uploaded(); console.log(this.selectors); // this.obtainAList(); this.dutyTableData = this.transferStation.filter(item => item.alarmWeekDay == this.week); console.log(this.dutyTableData) }, 3000), },
时间: 2024-02-14 17:06:16 浏览: 33
如果不希望在调用 `obtainAList()` 函数时触发 `watch` 中的 `selectors`,可以将 `selectors` 改为计算属性(computed property),然后在 `obtainAList()` 函数中修改计算属性 `selectors` 的值。这样在修改 `selectors` 的值时不会触发 `watch`,只有在计算属性的值被其他代码使用时才会触发计算属性的 getter 函数。
示例代码如下:
```
watch: {
// 将 selectors 改为计算属性
selectors: function() {
// debounce 函数中的代码移到这里
this.calendarComponentUp();
console.log(this.timeArray);
this.uploaded();
console.log(this.selectors);
this.dutyTableData = this.transferStation.filter(item => item.alarmWeekDay == this.week);
console.log(this.dutyTableData);
}
},
methods: {
obtainAList() {
// 修改计算属性 selectors 的值
this.selectors = new Date().getTime();
}
},
computed: {
// 计算属性 selectors
selectors: {
get() {
return this._selectors;
},
set(val) {
this._selectors = val;
}
}
}
```
在这个示例代码中,`selectors` 被改为计算属性,并且定义了一个 `_selectors` 变量来存储 `selectors` 的实际值。在 `watch` 中,只要 `_selectors` 发生改变,就会触发计算属性的 getter 函数,从而执行 debounce 函数中的代码。在 `obtainAList()` 函数中,只需要修改 `_selectors` 的值即可,不会触发 `watch` 中的代码。