1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| import Vue from 'vue'; import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({ state: { filter: 'all', todos: [ { id: 1, title: '完成作業', completed: false, editing: false, }, { id: 2, title: '繳交費用', completed: false, editing: false, }, ], }, getters: { remaining(state) { return state.todos.filter((todo) => !todo.completed).length; }, anyRemaining(state, getters) { return getters.remaining !== 0; }, filteredTodos(state) { switch (state.filter) { case 'all': return state.todos; case 'active': return state.todos.filter((todo) => !todo.completed); case 'completed': return state.todos.filter((todo) => todo.completed); default: return state.todos; } }, showClearCompletedTodo(state) { const completedTodo = state.todos.filter((todo) => todo.completed); return completedTodo.length > 0; }, }, mutations: { createTodo(state, todo) { state.todos.push({ id: todo.id, title: todo.title, }); }, clearCompletedTodo(state) { state.todos = state.todos.filter((todo) => !todo.completed); }, changeFilter(state, filter) { state.filter = filter; }, checkAllTodos(state, checked) { state.todos.forEach((todo) => { todo.completed = checked; }); }, doneEditTodo(state, todo) { const index = state.todos.findIndex((item) => item.id === todo.id); state.todos.splice(index, 1, { id: todo.id, title: todo.title, completed: todo.completed, editing: todo.editing, }); }, destroyTodo(state, index) { state.todos.splice(index, 1); }, }, actions: { createTodo(context, todo) { setTimeout(() => { context.commit('createTodo', todo); }, 0); }, clearCompletedTodo(context) { setTimeout(() => { context.commit('clearCompletedTodo'); }, 0); }, changeFilter(context, filter) { setTimeout(() => { context.commit('changeFilter', filter); }, 0); }, checkAllTodos(context, checked) { setTimeout(() => { context.commit('checkAllTodos', checked); }, 0); }, doneEditTodo(context, todo) { setTimeout(() => { context.commit('doneEditTodo', todo); }, 0); }, destroyTodo(context, index) { setTimeout(() => { context.commit('destroyTodo', index); }, 0); }, }, });
export default () => store;
|