【问题标题】:Nuxt vuex store not getting axios requestNuxt vuex 商店没有收到 axios 请求
【发布时间】:2026-02-07 13:35:02
【问题描述】:

已安装 Nuxt axios 模块并在 index.js 文件中工作,但是当我清空 index.js 文件并分离到它自己的模块文件 (team.js) 时它不起作用。

团队.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 模块的正确方法是什么?我见过的文档和其他项目应该可以工作

【问题讨论】:

标签: nuxt.js vuex-modules


【解决方案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')
  }
}

【讨论】:

  • 你是对的,你确实需要在 store/index.js 中有 nuxtServerInit 但上面的方法不起作用,因为你不能在这个文件之外调用它,并且每次加载都会调用一次 nuxtServerInit所以不能有重复。不知道我是怎么错过的,谢谢