已经安装了Nuxt axios模块并可以在index.js文件中工作,但是当我清空index.js文件并分离到其自己的模块文件(team.js)中时,它将无法工作。
Team.js
export const state = () => ({
teams: {},
})
export const mutations = {
SET_TEAMS (state, value) {
state.teams = value
}
}
export const actions = {
async nuxtServerInit ({ commit }) {
let {data} = await this.$axios.$get(`team`)
commit('SET_TEAMS', data)
}
在Vue开发工具中,它显示为- 球队:对象 球队:对象(空) 拥有Vuex模块的正确方法是什么?我已经看到的文档和其他项目应该可以工作
答案 0 :(得分:1)
在我的评论和原始建议之后,您的store/teams.js
文件可以拥有自己的nuxtServerInit
;但您可以随意命名,因为您需要从主模块(store/index.js
)中显式分配它。
store/teams.js
export const actions = {
async nuxtServerInit({ commit }) {
let {data} = await this.$axios.$get(`team`)
commit('SET_TEAMS', data)
}
}
}
store/index.js
export const actions = {
async nuxtServerInit({ dispatch }) {
dispatch('teams/nuxtServerInit')
}
}