我正在嘗試運行一個函數,該函數發出 GET 請求以更新產品數量,但僅當商店currentProduct屬性存在時。如果沒有選定的產品,那么我不想讓間隔無緣無故地觸發一個功能。在偽代碼中,我基本上是說:如果 $this.store.getters['myStore/getCurrentProduct']那么 setInterval(this.$store.dispatch('updateQuantity'), 2000);我唯一的想法是為 currentProduct 創建一個計算屬性:computed: { currentProd() { return $this.store.getters['myStore/getCurrentProduct']; }}然后看它:watch: { currentProd(newVal,oldVal) { if (newVal != null) { let foo = setInterval(this.$store.dispatch('updateQuantity'), 2000); } } }我只是不確定如何防止它重疊并且有大量的間隔觸發
Vuex:僅當存儲屬性存在時每 2 秒調度一個函數?
慕娘9325324
2023-03-24 14:09:48