source

외부 js 파일에서 저장소로 액세스(nuxtjs)

nicesource 2022. 12. 4. 22:31
반응형

외부 js 파일에서 저장소로 액세스(nuxtjs)

컴포넌트 외부의 파일에서 스토어에 액세스하려고 합니다.이 문제를 검색했을 때, 사람들이 내 파일에서 스토어를 Import하고 액세스 할 수 있다고 하는데, 이 작업을 실행할 수 없습니다.

저희 가게는 이렇게 지어졌습니다.

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

js파일로 Import를 시도하고 있습니다.

import store from '@/store'

좋은 생각 있어요?

다음과 같이 인스턴스화 시 스토어를 등록하여 가져올 수 있습니다.

외부 파일some-service.js:

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

언급URL : https://stackoverflow.com/questions/50316251/access-store-from-outside-js-file-in-nuxtjs

반응형