Push to vuex store array not working in VueJS Push to vuex store array not working in VueJS vue.js vue.js

Push to vuex store array not working in VueJS


You are trying to modify the vuex state from the vue component, You can not do it. You can only modify vuex store from a mutation

You can define a mutation like following:

var store = new Vuex.Store({  state: {    customers: [      { id: '1', name: 'user 1',},    ]  },  mutations: {     addCustomer (state, customer) {      // mutate state      state.customers.push(customer)    }  }})

Now you can commit this mutation from the vue instance, like following:

mounted: function() {      this.$http.get('http://localhost/facebook-login/api/get_customers.php')      .then(response => {        return response.data;      })      .then(data => {        store.commit('addCustomer', { id: '2', name: 'User 2'})      });    }