Should we use v-model to modify Vuex store?

前端 未结 5 2070
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 10:50

Hello I am beginner in Vue and I do have a problem that\'s really bugging me. I am wondering should we use v-model directive to modify vuex store? Vuex says that we should modif

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 11:15

    My Solution to this was to use a getter to set value and @input to call the mutation.

    
    

    getters.js:

    export default {
      apartmentStreet: state => state.apartment.street,
    };
    

    mutations.js

    export default {
      apartmentValue(state, payload) {
        let oldValue = state.apartment[payload.handle];
        let newValue = payload.value;
        if (newValue !== oldValue) state.apartment[payload.handle] = payload.value;
      }
    };
    

    If you use this method be sure to check which event you want.

提交回复
热议问题