将数组提交到 Vuex 存储突变 [英] Committing array to Vuex store mutation

查看:30
本文介绍了将数组提交到 Vuex 存储突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储文件:

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' 中只传递数组的第一个对象.

resp is an object array. When Vuex calls the setCases() mutation, only the first object of the array is delivered in 'items'.

这是为什么?

推荐答案

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

Looking at this: https://vuex.vuejs.org/guide/mutations.html [see Object-Style Commit section]

当使用对象样式提交时,整个对象将作为突变处理程序的有效负载,因此处理程序保持不变

When using object-style commit, the entire object will be passed as the payload to mutation handlers, so the handler remains the same

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

The resp value should be accessible as items.items in the mutation function.

这篇关于将数组提交到 Vuex 存储突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆