文章詳情頁
javascript - vue監聽data中的某一數組的某一項
瀏覽:30日期: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. python - 獲取到的數據生成新的mysql表2. javascript - js 對中文進行MD5加密和python結果不一樣。3. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat4. window下mysql中文亂碼怎么解決??5. sass - gem install compass 使用淘寶 Ruby 安裝失敗,出現 4046. python - (初學者)代碼運行不起來,求指導,謝謝!7. 為啥不用HBuilder?8. python - flask sqlalchemy signals 無法觸發9. python的文件讀寫問題?10. 為什么python中實例檢查推薦使用isinstance而不是type?
排行榜
