【问题标题】:Committing array to Vuex store mutation将数组提交到 Vuex 存储突变
【发布时间】: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 的情况下偶然发现这个问题:-)

标签: vue.js vuex


【解决方案1】:

看看这个:https://vuex.vuejs.org/guide/mutations.html [参见对象样式提交部分]

当使用对象样式提交时,整个对象将作为 有效载荷到突变处理程序,所以处理程序保持不变

resp 值应该可以在变异函数中作为items.items 访问。

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2021-02-19
    • 2020-06-06
    • 2019-08-17
    • 2021-02-03
    • 2020-03-08
    • 2019-03-08
    • 2019-05-02
    • 2018-10-03
    相关资源
    最近更新 更多