[Vue] Computed property "XXX" was assigned to but it has no setter.


閱讀這篇文章:https://blog.csdn.net/weixin_34090562/article/details/91369638

全選,通過計算屬性計算得來。結果報錯Computed property "XXX" was assigned to but it has no setter.

 1 // 總的全選
 2     handleSelectAllClassfiy(val) {
 3       if (val) {
 4         for (let i = 0; i < this.tabledata.length; i += 1) {
 5           this.tabledata[i].mychecked = true;
 6           this.firstChanged(i); // 調用一級change事件
 7         }
 8       } else {
 9         for (let i = 0; i < this.tabledata.length; i += 1) {
10           this.tabledata[i].mychecked = false;
11           this.firstChanged(i); // 調用一級change事件
12         }
13       }
14     },

 除了獲取計算屬性的值,還可以設置計算屬性的值,並且在設置過程中做一些操作。實現這一點需要將計算屬性由函數改為帶有get和set屬性的對象。

解決方法:

 1 allChecked: {
 2   get () {
 3       let count = 0;
 4       for (let i = 0; i < this.tabledata.length; i += 1) {
 5          if (this.tabledata[i].mychecked === true) {
 6             count += 1;
 7          } else {
 8             count -= 1;
 9          }
10       }
11       if (count === this.tabledata.length) {
12           return true;
13       }
14       return false;
15    },
16    set (val) {
17       return val;
18    }
19 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM