5656
5757<script lang="ts">
5858import { Component , Vue , Watch } from ' vue-property-decorator'
59+ import { Getter , Mutation } from ' vuex-class'
5960
6061import Todo from ' ./Todo'
6162
6263Vue .filter (' pluralize' , (n : number ): string => {
63- return n === 1 ? ' item' : ' items'
64+ return n === 1 ? ' item' : ' items'
6465})
6566
6667Vue .directive (' todo-focus' , (el , binding ) => {
67- if (binding .value ) {
68- el .focus ()
69- }
68+ if (binding .value ) {
69+ el .focus ()
70+ }
7071})
7172
7273@Component ({
7374 name: ' app'
7475})
7576export default class App extends Vue {
77+ @Getter (' all' ) todos
78+ @Getter remaining
79+
80+ @Mutation (' toggle' ) toggleTodo
81+ @Mutation (' remove' ) removeTodo
82+ @Mutation removeCompleted
83+
7684 private visibility: string = ' all'
7785
7886 private newTodo: string = ' '
@@ -85,18 +93,10 @@ export default class App extends Vue {
8593 window .addEventListener (' hashchange' , this .onHashChange )
8694 }
8795
88- get todos(): Todo [] {
89- return this .$store .getters .all
90- }
91-
9296 get filteredTodos(): Todo [] {
9397 return this .$store .getters [this .visibility ]
9498 }
9599
96- get remaining(): number {
97- return this .$store .getters .active .length
98- }
99-
100100 get allDone(): boolean {
101101 return this .remaining === 0
102102 }
@@ -116,10 +116,6 @@ export default class App extends Vue {
116116 this .newTodo = ' '
117117 }
118118
119- removeTodo(todo : Todo ) {
120- this .$store .commit (' remove' , todo )
121- }
122-
123119 editTodo(todo : Todo ) {
124120 this .beforeEditCache = todo .title
125121 this .editedTodo = todo
@@ -145,14 +141,6 @@ export default class App extends Vue {
145141 todo .title = this .beforeEditCache
146142 }
147143
148- toggleTodo(todo : Todo ) {
149- this .$store .commit (' toggle' , todo )
150- }
151-
152- removeCompleted() {
153- this .$store .commit (' removeCompleted' )
154- }
155-
156144 onHashChange() {
157145 const visibility = window .location .hash .replace (/ #\/ ? / , ' ' )
158146
0 commit comments