从nuxtjs中的外部js文件访问存储

Elo*_*ati 5 vue.js vuex nuxt.js

我正在尝试从组件外部的文件访问商店当我搜索这个问题时,我看到人们说我应该从我的文件中导入商店然后我可以访问它,但我不能这样做工作

我的商店是这样建造的:

const createStore = () => {
  return new Vuex.Store({
    state: { ... },
    getters: { ... },
    mutations: { ... },
    actions: { ... },
  })
}
Run Code Online (Sandbox Code Playgroud)

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

import store from '@/store'
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Ray*_*dus -1

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

外部文件some-service.js

export default class SomeService {
  constructor(store) {
    this.store = store
  }
  doStuff() {
    this.store.dispatch('someAction')
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 你能展示一下这个类是如何实例化的吗?“商店”起源于哪里? (7认同)