Full-Stack Web Development with Vue.js and Node

(singke) #1
Introducing Vuex Chapter 8

},
actions: {
fetchMovies: (context, payload) => {
axios({
method: 'get',
url: '/movies',
})
.then((response) => {
context.commit("MOVIES", response.data.movies);
})
.catch(() => {
});
}
}
})

In the preceding code, we have moved the axios part from the component. When we get a


successful response, we will commit a mutation called MOVIES, which then mutates the


value of the movies in the state.


Creating a mutation


Let's go on and add a mutation as well. In store.js, replace the contents with the


following code:


import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

Vue.use(Vuex);

export const store = new Vuex.Store({
state: {
movies: []
},
mutations: {
MOVIES: (state, payload) => {
state.movies = payload;
}
},
actions: {
fetchMovies: (context, payload) => {
axios({
method: 'get',
url: '/movies',
Free download pdf