vue.js - 从 nuxtjs 中的外部 js 文件访问存储
问题描述
我正在尝试从组件外部的文件访问商店当我搜索此问题时,我看到人们说我应该从我的文件中导入商店,然后我可以访问它,但我无法做到这一点工作
我的商店是这样建造的:
const createStore = () => {
return new Vuex.Store({
state: { ... },
getters: { ... },
mutations: { ... },
actions: { ... },
})
}
我已经尝试将它导入到我的 js 文件中,就像我看到的推荐的那样
import store from '@/store'
有任何想法吗?
解决方案
您可以通过在实例化时注册来导入商店,如下所示:
外部文件some-service.js
:
export default class SomeService {
constructor(store) {
this.store = store
}
doStuff() {
this.store.dispatch('someAction')
}
}
推荐阅读
- c++ - 具有对齐存储的动态内存分配
- java - java.lang.IllegalArgumentException:非法 base64 字符 -1
- angular - 从可观察的角度 7 中获取最后发出的值
- reactjs - 如何在 React 中的套接字事件后删除数组中的元素?
- android - 应用程序关闭时在后台检查互联网连接
- php - 使用 laravel 查询获取错误的 ID
- angular - Angular div 删除动画
- testing - 如何合并elixir伞app的测试报告?
- node.js - Node js 承诺异步/等待
- vb.net - 设置 combobox.dropdown = true 时combobox.text 更改