Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:state.categoriesState.push不是函数VUEX_Javascript_Vuejs2_Vuex - Fatal编程技术网

Javascript TypeError:state.categoriesState.push不是函数VUEX

Javascript TypeError:state.categoriesState.push不是函数VUEX,javascript,vuejs2,vuex,Javascript,Vuejs2,Vuex,我有这个错误,但我不明白原因,你能指导我吗 错误: TypeError:state.categoriesState.push不是函数 我的代码: state.js export default { categoriesState: [] } export function setCategories(state, category){ state.categoriesState.push(category); }; 突变.js export default {

我有这个错误,但我不明白原因,你能指导我吗

错误:

TypeError:state.categoriesState.push不是函数

我的代码:

state.js

export default {
    categoriesState: []
}
export function setCategories(state, category){
    state.categoriesState.push(category);    
};
突变.js

export default {
    categoriesState: []
}
export function setCategories(state, category){
    state.categoriesState.push(category);    
};
从我的组件调用**VUEX:**

methods: {
...mapMutations('cat', ['setCategories']),

        addCategoriesToVuex(category){
            this.setCategories(category);
        },
}

您的变量categoriesState很可能是一个对象,而不是数组。您应该首先通过对变量的类型进行控制台检查categoriesState

export function setCategories(state, category){
      console.log(typeOf(state.categoriesState));
    //state.categoriesState.push(category);    
};

您可以使用set方法向vue中的对象添加新属性。

我遇到了相同的问题,这是因为vuex persistedstate包。正如@OscarDev所提到的,变量名的更改起到了帮助作用。这是因为persistedstate在编写代码时保存了变量的类型。
因此,清除persistedstate缓存也有帮助。

它是一个数组,但我不知道为什么它会被视为一个对象,我只是将名称更改为vuex的状态,它就工作了。谢谢你。我也做了你做的同样的事,它起了作用@OscarDev