Skip to content

Instantly share code, notes, and snippets.

@oomusou
Created September 14, 2018 02:11
Show Gist options
  • Save oomusou/8ba38828cefd922cbc08f26a6c9486c7 to your computer and use it in GitHub Desktop.
Save oomusou/8ba38828cefd922cbc08f26a6c9486c7 to your computer and use it in GitHub Desktop.
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
Vue.use(Vuex);
const state = {
count: 0,
todos: [],
};
const mutations = {
setTodos: (state, payload) => state.todos = payload,
addItem: (state, payload) => state.todos.push({ title: payload, completed: false }),
finishItem: (state, payload) => state.todos[payload].completed = !state.todos[payload].completed,
};
const actions = {
fetchTodos({ commit }) {
const endpoint = 'https://jsonplaceholder.typicode.com/todos';
const response = res => commit('setTodos', res.data.slice(0, 5));
const error = e => console.log(e);
axios
.get(endpoint)
.then(response)
.catch(error);
},
};
export default {
state,
mutations,
actions,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment