[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 }