A
A
Alexander Koregin2020-01-10 20:07:07
Vue.js
Alexander Koregin, 2020-01-10 20:07:07

How to correctly add an element to an object in Vue.JS?

I am trying to add an element to an object but I am getting an error. The function that adds is called addLikedGood()
My code is:

mutations: {
    addLikedGood(state, id) {
      state.likedGood["id_product"] = id;
    }
  },
  getters: {
    getAuth: state => state.auth,
    getAllLikedGood: state => state.likedGood
  },
  state: {
    auth: false,
    likedGood: []
  }
};

5e18af390768f350255561.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
Lumore, 2020-01-11
@dragonika8

addLikedGood(state, id) {
      state.likedGood.push({ id_product: id });
}

A
Alex, 2020-01-10
@Kozack Vue.js

I don't fully understand what you are trying to do. Try using Vue.set

A
Aetae, 2020-01-10
@Aetae

likedGood: [] - array .
"id_product" - object key .
Are you sure this is what you want?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question