【问题标题】:Nuxt: Can't access vuex store from js fileNuxt:无法从 js 文件访问 vuex 存储
【发布时间】:2021-06-17 08:10:10
【问题描述】:

我正在 Nuxt 中创建一个带有 Vuex 状态管理的应用程序。问题是我想创建带有函数的 js 文件,我将把它导入到我的 Vue 文件中。我需要访问该文件中的商店。

在 functions.js 中,我想使用 import store from "@/store" 导入商店,但 store 给了我空对象。当我做import store from "@/store/dice.js",这是我想要访问的商店它返回未定义并在控制台中我得到信息:"export 'default' (imported as '$store') was not found in '~/store/dice'

@/store/index.js 为空。

@/store/dice.js 的结构类似于 Nuxt 教程:

export const state = () => ({
list: []
})

export const mutations = {
add(state, text) {
state.list.push({
text,
done: false
})
},
remove(state, { todo }) {
state.list.splice(state.list.indexOf(todo), 1)
},
toggle(state, todo) {
todo.done = !todo.done
}
}

谁能帮忙?

【问题讨论】:

    标签: nuxt.js vuex


    【解决方案1】:

    位于store 目录的Vuex Store 文件将在构建时被Nuxt 转换为一个store 实例。

    因此,您无法将其中的任何文件导入到商店外的其他 js 文件中。

    你必须解决:

    1. 推荐:将您的function.js 文件夹添加到您的商店并在那里使用您的函数(在突变、动作等中调用它们...)
    2. 不建议这样做:function.js 中调用您的函数时,将您的商店实例作为参数传递,然后您就可以使用它了。

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 1970-01-01
      • 2018-05-28
      • 2022-08-19
      • 2020-05-29
      • 2019-05-02
      • 2020-04-28
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多