Co-authored-by: coffeemil <coffeemil@vip.qq.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
count: 0,
name: 'Eduardo',
isAdmin: true,
+ items: [],
+ hasChanged: true,
}
},
})
However, some mutations are really hard or costly to apply with this syntax: any collection modification (e.g. pushing, removing, splicing an element from an array) requires you to create a new collection. Because of this, the `$patch` method also accepts a function to group this kind of mutations that are difficult to apply with a patch object:
```js
-cartStore.$patch((state) => {
+store.$patch((state) => {
state.items.push({ name: 'shoes', quantity: 1 })
state.hasChanged = true
})
count: 0,
name: 'Eduardo',
isAdmin: true,
+ items: [],
+ hasChanged: true,
}
},
})
不过,用这种语法的话,有些变更真的很难实现或者很耗时:任何集合的修改(例如,从数组中推送、移除、拼接一个元素)都需要你创建一个新的集合。因此,`$patch` 方法也接受一个函数来组合这种难以用补丁对象实现的变更。
```js
-cartStore.$patch((state) => {
+store.$patch((state) => {
state.items.push({ name: 'shoes', quantity: 1 })
state.hasChanged = true
})