[英]vuex unknown action type: Nuxtjs
I'm trying to fetch an action in a fetch hook in a pagee of my nuxt application but I get [vuex] unknown action type
error.我正在尝试在我的 nuxt 应用程序的页面中的 fetch 挂钩中获取一个动作,但我收到[vuex] unknown action type
错误。 This is my folder setup:这是我的文件夹设置:
This is my store/index.js
:这是我的store/index.js
:
import vue from "vue";
import Vuex from "vuex";
import axios from "axios";
import Vue from "vue";
import liveEvents from "./liveEvents";
Vue.use(Vuex, axios);
const store = () => {
return new Vuex.Store({
modules:{
liveEvents
}
})
}
export default store
this is my store/liveEvents/index.js
:这是我的store/liveEvents/index.js
:
const state = () => ({
eventsList: []
});
const actions = () => ({
async eventsList({ commit }) {
// the actions ...
},
const mutations = () => ( {
SET_EVENTLIST(state, events) {
state.eventsList = events;
},
const getters = () => ({
});
export default {
namespaced: true,
state,
actions,
mutations,
getters
}
And this is how I call it in my page:这就是我在页面中的称呼:
async fetch() {
const { store, error } = this.$nuxt.context;
try {
await store.dispatch("liveEvents/eventsList", null, { root: true });
} catch (e) {
error({
message: "error"
});
}
}
And this is the error I get:这是我得到的错误:
[vuex] unknown action type: liveEvents/eventsList
How can I fix this error?我该如何解决这个错误?
Nuxt automatically transforms store/**/*.js
files into Vuex modules , so you don't need to setup your own store in store/index.js
, and it should be removed. Nuxt 自动 将store/**/*.js
文件转换为 Vuex modules ,因此您无需在store/index.js
中设置自己的 store,应该将其删除。
Also, your actions
, mutations
, and getters
currently return a function that returns an object, but that should only be done for state
.此外,您的actions
、 mutations
和getters
当前返回 function ,该 function 返回 object ,但这仅适用于state
。 Instead, they should be objects:相反,它们应该是对象:
// store/liveEvents/index.js
export const state = () => ({
eventsList: []
});
export const actions = {
async eventsList({ commit }) {
// the actions ...
},
}
export const mutations = {
SET_EVENTLIST(state, events) {
state.eventsList = events;
},
}
export const getters = {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.