文章詳情頁
javascript - vue監(jiān)聽data中的某一數(shù)組的某一項(xiàng)
瀏覽:86日期:2023-02-27 13:59:21
問題描述
如圖我要監(jiān)聽items.amount,除了for循環(huán)的寫法,還有什么更簡單的寫法嗎?
問題解答
回答1:computed: { totalAmount () { // 計(jì)算出 items 數(shù)組中的 amount 總額 return this.items.reduce((a, b) => ({ amount: a.amount + b.amount })).amount }},watch: { totalAmount (newVal) { // 當(dāng)計(jì)算屬性變更時觸發(fā)更新 console.log(’amount change to ’, newVal) }}回答2:
個人覺得,你的總額的money應(yīng)該改成一個計(jì)算屬性
computed: { money() { let sum = 0; this.items.forEach(item => { sum += item.amount; }); return sum; }}
然后將money屬性從data中刪除,同時刪除你的watch
標(biāo)簽:
JavaScript
相關(guān)文章:
1. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問題2. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.3. docker - 如何修改運(yùn)行中容器的配置4. python3.x - python連oanda的模擬交易api獲取json問題第五問5. nignx - docker內(nèi)nginx 80端口被占用6. css3 - 沒明白盒子的height隨width的變化這段css是怎樣實(shí)現(xiàn)的?7. javascript - webpack熱加載配置不生效8. javascript - QWebEngineView 如何爬 angular 的動態(tài)數(shù)據(jù)?9. java - SSH框架中寫分頁時service層中不能注入分頁類10. java - instance method中 static后的<K>是什么意思?
排行榜

熱門標(biāo)簽