Update data using vuex

前端 未结 4 2041
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:17

    ES6 way of updating

    //mutations UPDATE:
    [mutationType.UPDATE_CATEGORY] (state, payload) {
        state.categories = [
            ...state.categories.map(item => 
                item.id !== updatedItem.id ? item : {...item, ...updatedItem}
            )
        ]
    }
    
    //mutations CREATE:
    [mutationType.CREATE_CATEGORY] (state, category) {
        state.categories = [category, ...state.categories] //Append to start of array
    }
    
    //mutations DELETE:
    [mutationType.DELETE_CATEGORY] (state, id) {
        state.categories = [
           ...state.categories.filter((item) => item.id !== id)
        ];
    }
    

提交回复
热议问题