【问题标题】:access store from outside js file in nuxtjs从 nuxtjs 中的外部 js 文件访问存储
【发布时间】:2018-10-23 06:48:08
【问题描述】:

我正在尝试从组件外部的文件访问存储 当我搜索这个问题时,我看到人们说我应该从我的文件中导入商店,然后我可以访问它,但我无法做到这一点

我的商店是这样建造的:

const createStore = () => {
  return new Vuex.Store({
    state: { ... },
    getters: { ... },
    mutations: { ... },
    actions: { ... },
  })
}

我已经尝试将它导入到我的 js 文件中,就像我看到的推荐的那样

import store from '@/store'

有什么想法吗?

【问题讨论】:

    标签: vue.js vuex nuxt.js


    【解决方案1】:

    您可以通过在实例化时注册来导入商店,如下所示:

    外部文件some-service.js:

    export default class SomeService {
      constructor(store) {
        this.store = store
      }
      doStuff() {
        this.store.dispatch('someAction')
      }
    }
    

    【讨论】:

    • 你能展示这个类是如何实例化的吗? store 来自哪里?
    猜你喜欢
    • 2022-08-19
    • 2014-11-20
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2018-03-31
    • 2014-09-05
    • 2018-05-28
    相关资源
    最近更新 更多