source

모듈 vuex js 저장소에서 전역 변환을 설정하는 방법

goodcode 2022. 7. 26. 23:41
반응형

모듈 vuex js 저장소에서 전역 변환을 설정하는 방법

글로벌 변수의 상태를 변경할 수 있어야 합니다.alert모든 Vuex 모듈에서 사용할 수 있습니다.

store/index.syslog:

export const state = () => ({
    alert: null 
})

store/child.syslog:

export const mutations = {
    SET_ALERT: function (rootState, alert) {
        rootState.alert = alert
    }
}
export const actions = {
    setalert({commit}){
        commit('SET_ALERT', 'warning')
    }
}

전화하고 싶다setalert글로벌하게 설정할 수 있습니다.store.state.alert로."warning".현재의,store.state.child.alert할 준비가 되어 있다"warning"대신.

다른 모듈의 변환 내에서 vuex 모듈 상태에 액세스할 수 없습니다.

지금 당신의SET_ALERT변환이 참조되고 있습니다.child안에 있기 때문에 말하다child모듈 범위상태 객체의 매개 변수 이름 변경rootState변하지 않을 거야

단, 간단하게 이동시킬 수 있습니다.SET_ALERT속에 있는 돌연변이index.js이 변환은, 여전히, R&D 로부터 호출할 수 있습니다.child모듈setalert액션.


모듈에 네임스페이스를 사용하는 경우(namespace: true)의 루트 모듈을 사용하도록 명시적으로 지시해야 합니다.commit콜, 이런 식으로:

commit('SET_ALERT', 'warning', { root: true });

Vuex 모듈용 매뉴얼은 다음과 같습니다.

언급URL : https://stackoverflow.com/questions/44786962/how-to-set-global-mutation-in-module-vuex-js-store

반응형