Javascript TypeError:state.categoriesState.push不是函数VUEX
我有这个错误,但我不明白原因,你能指导我吗 错误: TypeError:state.categoriesState.push不是函数 我的代码: state.jsJavascript 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 {
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