Vue.JS Vuex 存储并通过依赖注入访问我的 api

Vue.JS Vuex store and accessing my api through dependency injection

我无法访问我的商店模块中的 api。我已经创建了我的 api 的一个实例,就像商店一样在我的 vue 创建中传递了它。但是,当尝试在我的模块中实现逻辑时,this.$api 无法像在我的组件中那样工作。有什么方法可以访问我已经创建的 api 实例吗?

const api = new Api();


/* eslint-disable no-new */
new Vue({
    components: {App},
    router,
    api,               // <--- I want this.
    store,             // <--- To be accesable in the modules of this
    template: '<App/>'
}).$mount('#app');

那么我可以访问 api 实例而不在我的模块或存储中创建新实例吗?

我想你应该可以直接注入 api 存储,像这样:

const store = new Vuex.Store();
const $axios = axios.create();
store.$axios = $axios;
new Vue({
  components: {App},
  router,
  store,
  template: '<App/>'
}).$mount('#app');

无论如何,对于 Axios,它工作正常:https://forum.vuejs.org/t/accessing-axios-in-vuex-module/29414/3