案例 - 需要在更改标题复选框时选择/取消选择所有复选框。
问题 - 我可以发出和接收事件。可以看到我修改后的数据数组。但我猜有些 react 性在 Vue 中不起作用。
我已经在 CodeSandbox 上列出了非常小的剪切示例 - https://88nz9z64j0.codesandbox.io/
代码 -
https://codesandbox.io/s/88nz9z64j0
注意 - 这与数组突变有关。因为我可以选择各个复选框,并且在选择所有复选框后可以看到我的标题复选框标记为选中。然而,逆转并不是通过相同的机制发生的。多么奇怪!
必须从 Array(100).fill(length) 中学习作为 react 数据,否则我会做一些愚蠢的事情。
最佳答案
问题在于 Array.fill
正在就地修改数组。 Vue can't detect changes to an array when you directly modify its elements 。您需要先使用split
创建一个新数组,修改新创建的数组,然后重新分配它。
this.checkSelections = this.checkSelections.splice(0).fill(allSelected);
关于javascript - 通过 $emit 选择所有复选框不会重新渲染 View - Vuejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949859/