sourcecode

종속성 주입을 통해 Vue.JS Vuex 저장 및 my api

copyscript 2022. 8. 13. 12:25
반응형

종속성 주입을 통해 Vue.JS Vuex 저장 및 my api

스토어 모듈 내에서 API에 액세스하는 데 문제가 있습니다.my api의 인스턴스를 생성하여 스토어와 마찬가지로 vue 작성 내에서 전달했습니다.그러나 내 모듈에 로직을 구현하려고 할 때, 이것은.$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

언급URL : https://stackoverflow.com/questions/57095699/vue-js-vuex-store-and-accessing-my-api-through-dependency-injection

반응형