【发布时间】:2019-09-25 22:44:54
【问题描述】:
存储文件:
state: {
...
cases: [],
...
mutations: {
setCases(state, items) {
// items contains only the first object in the array
...
组件:
// resp is an array received from axios.get:
this.$store.commit({
type: 'setCases',
items: resp
})
resp 是一个对象数组。当 Vuex 调用 setCases() 突变时,只有数组的第一个对象在 'items' 中传递。
这是为什么呢?
【问题讨论】:
-
看这个:vuex.vuejs.org/guide/mutations.html [参见对象样式提交部分] ``` 当使用对象样式提交时,整个对象将作为有效负载传递给突变处理程序,因此处理程序保留同样的```
resp值应该可以在变异函数中作为items.items访问,对吧?你能证实这确实是这样吗? -
伟大的@gvk,非常感谢,你拯救了我的一天!
-
酷。添加作为答案,以防其他人在没有完全 RTFM 的情况下偶然发现这个问题:-)