文章詳情頁
javascript - vue監聽data中的某一數組的某一項
瀏覽:135日期:2023-02-27 13:59:21
問題描述
如圖我要監聽items.amount,除了for循環的寫法,還有什么更簡單的寫法嗎?
問題解答
回答1:computed: { totalAmount () { // 計算出 items 數組中的 amount 總額 return this.items.reduce((a, b) => ({ amount: a.amount + b.amount })).amount }},watch: { totalAmount (newVal) { // 當計算屬性變更時觸發更新 console.log(’amount change to ’, newVal) }}回答2:
個人覺得,你的總額的money應該改成一個計算屬性
computed: { money() { let sum = 0; this.items.forEach(item => { sum += item.amount; }); return sum; }}
然后將money屬性從data中刪除,同時刪除你的watch
標簽:
JavaScript
相關文章:
1. 微信小程序的選項卡中的數據分頁加載卡頓問題???2. node.js - node -v拒絕訪問3. android 手勢解鎖 布局問題4. javascript - 父選項卡bug,子選項卡好用,求解?5. css3 - 如何出現橫向捲軸?6. javascript - transition height auto 過渡動畫7. javascript - 請問一下組件的生命周期beforeDestory是在什么情況下面觸發的呢?8. intellij-idea - 嘗試Scala on Android過程中SBT生成的MainActivity.scala出現錯誤,如何解決?9. html5 - 急求?被公司問住了10. jquery - Ajax請求,返回200狀態碼,但收到數據包為空值
排行榜
