How to handle Uncaught Error: [nuxt] store/index.js should export a method that returns a Vuex instance

How to handle Uncaught Error: [nuxt] store/index.js should export a method that returns a Vuex instance

我已经在 Nuxt in store/index.js as the documentation 推荐中设置了一个默认商店。当我尝试呈现我的应用程序时,出现以下错误:

Uncaught Error: [nuxt] store/index.js should export a method that returns a Vuex instance.

我的 store/index.js 文件如下所示:

import Vuex from 'vuex'
import Vue from 'vue'
import myModule from './myModule'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: () => ({

  }),
  mutations: {},
  actions: {},
  modules: {
    myModule: myModule
  }
})
export default store

我该如何处理?

您正在将 Vuex 存储导出为常量,您应该导出一个默认方法,returns Vuex 存储实例。

您的 store/index.js 文件应如下所示:

import Vuex from 'vuex'
import Vue from 'vue'
import myModule from './myModule'

Vue.use(Vuex)

export default () => new Vuex.Store({
  state: () => ({

  }),
  mutations: {},
  actions: {},
  modules: {
    myModule: myModule
  }
})

我确实有以下内容,而且效果很好。

import { test } from './modules/tasty_module'

const state = () => ({})
const mutations = {}
const actions = {}
const getters = {}

export default {
  state,
  mutations,
  getters,
  actions,
  modules: {
    testModule: test,
  },
}