programing

vuejs2의 다른 모듈 뮤테이터를 사용하여 모듈 내의 스테이트 프로펠을 변환하는 것이 가능합니까?

randomtip 2022. 7. 14. 21:23
반응형

vuejs2의 다른 모듈 뮤테이터를 사용하여 모듈 내의 스테이트 프로펠을 변환하는 것이 가능합니까?

예를 들어 다음과 같습니다.

const moduleA = {
  state: { name: 'Cristian' },
  namespaced: true,
}

const moduleB = {
  state: { color: 'red' },
  namespaced: true,
}

const store = new Vuex.Store({
  state: { user: null, },
  modules: {
    a: moduleA,
    b: moduleB
  },
  mutations: {
    setPropValue(state, payload) {
      for (let [key, value] of Object.entries(payload)) {
        state[key] = value;
      }
    },
  },
})

내 vue 인스턴스에는 사용자가 로그인/아웃하고 그에 따라 상태를 변경할 때 파이어베이스 옵서버가 있습니다.그래서 제가 이루고 싶은 것은 다음과 같습니다.

const payload = {
  module: 'root',
  payload: { user, },
}

this.$store.commit('setPropValue', {payload});

다른 모듈의 경우:

const payload = {
  module: 'a',
  payload: { name: 'Alexander', },
}

같은 로직을 실행하지만 부하가 달라 모듈a에서 소품을 변경합니다.

this.$store.commit('setPropValue', {payload});

변환에 직접 커밋하는 대신 액션을 사용하는 것을 고려합니다.

this.$store.dispatch('setPropValue', { user });

그러면 vuex에서 당신의 액션은

actions: {
  setPropValue({ commit }, { user }) {
    commit("a/setName", user.name);
    commit("b/setColor", user.favoriteColor);
  }
}

각 모듈은 모듈 상태를 수정하기 위해 자체 '로컬' 변환이 필요합니다.

상세한 것에 대하여는, https://vuex.vuejs.org/en/modules.html 를 참조해 주세요.

언급URL : https://stackoverflow.com/questions/45683990/is-it-possible-to-mutate-a-state-prop-in-a-module-using-another-s-module-mutator

반응형