forked from vuejs-tips/vuex-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuex-todo.html
70 lines (62 loc) · 1.51 KB
/
vuex-todo.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vuex TODO</title>
</head>
<body>
<div id="app">
<h3>All Todos</h3>
<ol>
<li v-for="todo in allTodos" @click="toggleTodo(todo.id)">
{{todo.label}}
</li>
</ol>
<h3>Done Todos</h3>
<ol>
<li v-for="todo in doneTodos">
{{todo.label}}
</li>
</ol>
</div>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script>
const store = new Vuex.Store({
state: {
todos: [{
id: 1,
label: 'Buy Milk',
done: false
}]
},
getters: {
allTodos: state => state.todos,
doneTodos (state, getters, rootState) {
return state.todos.filter(todo => todo.done)
},
getTodoById (state, getters, rootState) {
return id => state.todos.find(todo => todo.id === id)
}
},
mutations: {
toggleTodo (state, todo) {
todo.done = !todo.done
}
},
actions: {
toggleTodo ({commit, getters}, id) {
todo = getters.getTodoById(id)
commit('toggleTodo', todo)
}
}
})
new Vue({
el: '#app',
store,
computed: Vuex.mapGetters(['allTodos', 'doneTodos']),
methods: Vuex.mapActions(['toggleTodo'])
})
</script>
</body>
</html>