vuejs2 - How can my vuex mutation dispatch a new action? -
how can vuex mutation dispatch new action or how can action read access store?
basically i've got action calls mutation:
updateselecteditems: (context, payload) => { context.commit('updateselecteditems', payload); }, and mutation updates list. gets new items. need these new items:
updateselecteditems: (state, payload) => { var newitems = _.differencewith(payload, state.selecteditems, function (a, b) { return a.name === b.name; }); state.selecteditems = _.clonedeep(payload); _.each(newitems, (item) => { // how do this?? context.dispatch('getitemdetail', item.name) }); },
it's not best practice make mutations much. it's best if they're super-simple , 1 thing. let actions take care of multi-step processes might affect state.
your example make more sense structured this:
actions: { updateselecteditems(context, payload) { var selecteditems = context.state.selecteditems; var newitems = _.differencewith(payload, selecteditems, (a, b) => { return a.name === b.name; }); context.commit('setselecteditems', payload); _.each(newitems, (item) => { context.dispatch('getitemdetail', item.name) }); }, getitemdetail(context, payload) { // ... } }, mutations: { setselecteditems(state, payload) { state.selecteditems = _.clonedeep(payload); } } if need dispatch inside mutation (which i'd highly recommend not doing), can pass dispatch function mutation part of payload.
Comments
Post a Comment