반응형
Vue 플러그인 설치 기능에 Vuex를 추가하고 구성 요소 간에 상태를 공유하는 방법이 있는가?
기본적으로 vue 설치 및 vue 구성 요소에서 vue 플러그인을 통해 추가되는 내 스토어를 사용하고 싶다.
실제 설치 기능 내에서 vuex를 사용할 수 있고 vuex가 할 수 있는 모든 작업을 수행할 수 있다는 것을 발견했지만, 사용자 지정 구성 요소에 전달할 수는 없는 것 같아.
내 SFC에 글로벌 이벤트를 추가하는 다른 방법은 없으십니까?
import MyComponent from './MyComponent.vue'
import MySecondComponentfrom './MySecondComponent.vue'
import Vuex from 'vuex'
import store from './store.js'
const CustomFn = function(message, options) {
this.$store.dispatch("someRandomAction", { message, options })
}
export default {
install(Vue, options) {
if (this.installed) { return }
this.installed = true
Vue.use(Vuex)
const storeInstance = new Vuex.Store(store)
Vue['store'] = storeInstance
// is there a way to add storeInstance to my own components and share one state between them?
Vue.component('MyComponent', Vue.extend(MyComponent))
Vue.component('MySecondComponent', Vue.extend(MySecondComponent))
Vue.prototype['$doStuff'] = CustomFn
Vue['doStuff'] = CustomFn
}
}
문제는 MyComponent 내에서 이 Vuex 스토어 인스턴스를 통과하지 못하고 여러 구성 요소 간에 동일한 스토어를 공유할 수 없다는 점이다.
사용자 지정 구성 요소 속성은 Vue 2에 할당하여 전체적으로 제공되어야 함Vue.prototype
구성요소 인스턴스(instance)는 그것으로부터 원형적으로 상속되기 때문이다.
new Vuex.Store(store)
에 대한 실수다.store
이미 Vuex 저장소가 있으며 저장소 옵션을 포함하는 일반 개체가 아님.이것은 이 질문에서 보이는 것과 같이 상점을 오작동하게 할 것이다.라 할지라도store
아직 저장 인스턴스가 아니므로, 인스턴스를 하나의 인스턴스로 만들어 별도 모듈로 내보내는 것이 타당하여, 비 컴포넌트 모듈로 가져올 수 있다.
다음과 같아야 한다.
Vue.prototype.$store = store
반응형
'programing' 카테고리의 다른 글
C와 C++에서 'constant static'은 무엇을 의미하는가? (0) | 2022.05.16 |
---|---|
C/C++ 프로그램과 그 플러그 인 DLL을 위한 최고의 무료 메모리 누수 검출기는 무엇인가? (0) | 2022.05.15 |
chai 및 vue-test-utilities를 사용하여 Vue 구성 요소 테스트 (0) | 2022.05.15 |
16진수 문자열(char [])을 int로 변환하시겠습니까? (0) | 2022.05.15 |
64비트 Windows에서 긴 비트 크기 (0) | 2022.05.15 |