Update data using vuex

前端 未结 4 2038
Happy的楠姐
Happy的楠姐 2021-01-02 14:39

As Vuex, I\'m trying to update an object using form. My code like this.

In store:

const state = {
   categories: []
};

//mutations:
[mutationType.UP         


        
4条回答
  •  半阙折子戏
    2021-01-02 15:29

    and much more simple, just use methods to modify arrays (never modify them directly) (check this: https://vuejs.org/2016/02/06/common-gotchas/#Why-isn%E2%80%99t-the-DOM-updating even old and DOM related, is still valid)

    So just find your object and replace with splice in your mutation:

    const index = state.objectsArray.map(o => o.id).indexOf(newObject.id);
    state.objectsArray.splice(index, 1, newObject);
    

提交回复
热议问题