vuex use state in action 1

vuex use state in action

// If you want to use state in action when using Vuex store
// all you need is to add 'state' next to commit in curly bracket '{ }'

// Exapmle:

someAction({commit, state}){
            axios.get("https://myUrl.com/" + state.version )
            .then((response) => {
                commit('champions', {
                    champions: response.data.data
                })
            })
            .catch(function (error) {
                console.log(error);
            })
        }

Here is what the above code is Doing:
1. We are using axios to make a get request to the url.
2. We are using the state to get the version of the API.
3. We are using the commit to commit the data to the store.
4. We are using the response.data.data to get the data from the response.
5. We are using the champions mutation to commit the data to the store.

Similar Posts