Vuex状态的长度不出来

时间:2019-08-25 13:01:24

标签: vue.js vuex

vuex中有一个状态。

我将'images'定义为一个数组,当我通过console.log(state.images)打印时,结果如下所示。

[__ob__: Observer]
0: {…}
1: {…}
2: {…}
3: {…}
4: {…}
length: 5
__ob__: Observer {value: Array(5), dep: Dep, vmCount: 0}
__proto__: Array

在那之后,当我使用console.log(state.images[0])console.log(state.images.length)时不起作用。您能推荐一些解决方案吗?非常感谢您阅读本文。

编辑)我将详细编写更多代码。

const state = {
    images:[]
};

const mutations = {
    pushPresentImage(state, yo) {
        state.images.push(yo);
},

const actions = {
    imageLoad() {

    },

    imageLength({commit}, image){
        axios.post('/', ...)
           .then(result =>{ 
                commit('pushPresentImage', result.data.image)
            })
           .then(() => {
                console.log(state.images) // it works well like above.
                console.log(state.images[0]) // it says undefined.. 
            })
},

1 个答案:

答案 0 :(得分:1)

似乎当vuex未完全保存状态时,您正在读取INFO Client:54 - Preparing resources for our AM container 数组。但是我尝试使用与您使用的相同的代码,它对我有用。尝试重组您的州,这样它就不会单独存在:

state.images[0]

这是codesandbox上的示例代码

相关问题