Programing

전체 개체 대신 Vue 스토어 디스패치 단일 속성

c10106 2022. 3. 23. 20:02
반응형

전체 개체 대신 Vue 스토어 디스패치 단일 속성

vuex 매장에 전체 물건 대신 하나의 물건만 보내는 것이 가능한지 궁금했다.vuex 문서에는 가능성이 없다.현재 다음과 같은 개체를 저장함:

store.dispatch('currentUser', {
  memberData: 'fooBar'
});

어떤 때는 단지 데이터베이스에서 하나의 값만 가져와 저장소에 넣고 싶을 때가 있다.그렇게 할 수 있는 방법이 있을까?

갱신하다

내 질문은 명확하지 않은 것 같아.

사실 나는 서브스크립션 중인 memberData의 하위 중첩된 속성에 액세스해야만 memberData의 한 요소만 변경할 수 있다.솔직히, 반응이 어떤지는 중요하지 않다.'foo'일 수도 있다.

발송을 위해 문서를 검토하는 경우 두 번째 인수payload는 어떤 유형도 될 수 있다.자산에 중첩된 객체 스타일의 디스패치가 반드시 필요한 것은 아니다.

발송:

store.dispatch('currentUser', response[0]);

저장:

state: {
  currentUser: undefined
},
mutations: {
  setCurrentUser(state, currentUser) {
    state.currentUser = currentUser;
  }
},
actions: {
  currentUser(context, payload) {
    context.commit('setCurrentUser', payload);
  }
}

돌연변이:

setCurrentUser(state, currentUser) {
  state.currentUser = currentUser;
}

여기 실천의 가 있다.

업데이트:

대신 변경사항을 병합/업데이트하는 것이 목적이라면 객체 리터럴에서 스프레드를 사용할 수 있다.이는 Vuex 설명서에서 Vue의 반응도 규칙을 따르는 돌연변이에 대해 언급되기도 한다.

발송:

store.dispatch('currentUser', { systemLanguage: response[0].systemLangId });

저장:

state: {
  memberData: { systemLanguage: 'foo' }
},
mutations: {
  updateCurrentUser(state, updates) {
    state.memberData = { ...state.memberData, ...updates };
  }
},
actions: {
  currentUser(context, payload) {
   context.commit('updateCurrentUser', payload);
  }
}

여기 실제 행동의 가 있다.

그게 도움이 되길 바래!

참조URL: https://stackoverflow.com/questions/54658172/vue-store-dispatch-single-property-instead-whole-object

반응형