38

I'm using Vuex to show a list of users from 'store.js'. That js file has array like this.

var store = new Vuex.Store({
  state: {
    customers: [
      { id: '1', name: 'user 1',},
    ]
  }
})

I want to insert a new set of values to the same array

{ id: '1', name: 'user 1',}

The above values are obtained from a URL (vue-resource). Below is the code to push the obtained data to the array. However, the data is not inserting

mounted: function() {
      this.$http.get('http://localhost/facebook-login/api/get_customers.php')
      .then(response => {
        return response.data;
      })
      .then(data => {
        store.state.customers.push(data) // not working!!
        console.log(data) // prints { id: '2', name: 'User 2',}
        store.state.customers.push({ id: '2', name: 'User 2',})
      });
    }
4
  • get_customers.php returns a array or a single user? Commented Jan 24, 2017 at 14:32
  • it works if you put like this? store.state.customers.push({data}) Commented Jan 24, 2017 at 14:34
  • @rogeriolino actually it should return entire array of users. But returning single user is also enough. I was trying both. None of them worked Commented Jan 24, 2017 at 16:32
  • @Miguel tried that. Not working Commented Jan 24, 2017 at 16:33

1 Answer 1

60

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'})
      });
    }
Sign up to request clarification or add additional context in comments.

8 Comments

that's not true. The value in a vuex store can be changed from Vue component. I've done that. For eg: "store.state.something = 1" will work
@GijoVarghese The first line in docs says: "The only way to actually change state in a Vuex store is by committing a mutation."
Anyway thanks @Saurabh I figured it out finally. The syntax of json returned from the url from wrong. That was the error!!. It returned name instead of "name"
@GijoVarghese the whole purpose of vuex is lost when you modify your stateful data directly within your vue component...
It might work, but it comes under the category of "undefined behavior". Its a misuse of the framework, and you can expect it to stop working pretty much as soon as the devs realise that there might be a loophole, because the correct behavior is to throw an error and try and prevent it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.