Skip to content

Commit edb55eb

Browse files
committed
lesson03.完成清除完成Todos方法
1 parent 7d4f091 commit edb55eb

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

README.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,12 @@ created () {
833833
export default {
834834
name: 'footer',
835835
// 声明 props
836-
props: ['remaining'],
836+
props: ['todos'],
837+
computed: {
838+
remaining: function () {
839+
return this.todos.filter(todo => !todo.completed).length
840+
}
841+
},
837842
filters: {
838843
pluralize: function (n) {
839844
return n === 1 ? 'item' : 'items'
@@ -842,18 +847,11 @@ created () {
842847
}
843848
</script>
844849
````
845-
改造`App.vue`,传递remaining到子组件
850+
改造`App.vue`,传递todos到子组件
846851
````html
847-
<MyFooter :remaining="remaining"/>
848-
````
849-
定义计算属性
850-
````javascript
851-
computed: {
852-
remaining: function () {
853-
return this.todos.filter(todo => !todo.completed).length
854-
}
855-
}
852+
<MyFooter :todos="todos"/>
856853
````
854+
857855
#### 双击编辑 Todo
858856
对`Todos.vue`进行完善
859857
1. 首先我们需要一个data记录哪一个todo属于当前编辑状态,在data中增加`editedTodo: null`
@@ -936,4 +934,31 @@ directives: {
936934
<button class="destroy" @click="removeTodo(todo)"></button>
937935
````
938936
939-
#### 快捷删除 Complete Todos
937+
#### 快捷删除 Complete Todos
938+
首先,我们分析一下,`Footer.vue`中的`todos`是父组件`App.vue`传过去的,所以子类不能直接删除,需要在父组件`App.vue`定义对应的删除方法
939+
点击clear complete,首先需要定义一个删除的方法removeCompleted,这里需要注意的是,需要对原数组进行删除,对应的`watch`才会生效,如果直接替换this.todos,是不会触发todos的观察更新的
940+
````javascript
941+
removeCompleted: function () {
942+
const completedTodos = this.todos.filter(todo => todo.completed)
943+
completedTodos.forEach(todo => this.todos.splice(this.todos.indexOf(todo), 1))
944+
}
945+
````
946+
向子组件传递事件
947+
````html
948+
<MyFooter @removeCompletedHandle="removeCompleted" :todos="todos"/>
949+
````
950+
951+
修改子组件`Footer.vue`,以支持对应的事件接口
952+
````javascript
953+
methods: {
954+
removeCompleted: function () {
955+
this.$emit('removeCompletedHandle')
956+
}
957+
},
958+
````
959+
绑定方法到模板中,当有所有Todos大于未完成Todos时候显示`v-show="todos.length > remaining"`
960+
````html
961+
<button class="clear-completed" @click="removeCompleted" v-show="todos.length > remaining">
962+
Clear completed
963+
</button>
964+
````

lesson/03.vue-todos/src/App.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<MyHeader @addTodoHandle="addTodo" />
44
<!-- 路由匹配到的组件将渲染在这里 -->
55
<router-view></router-view>
6-
<MyFooter :remaining="remaining"/>
6+
<MyFooter @removeCompletedHandle="removeCompleted" :todos="todos"/>
77
</div>
88
</template>
99

@@ -36,11 +36,10 @@
3636
title: value,
3737
completed: false
3838
})
39-
}
40-
},
41-
computed: {
42-
remaining: function () {
43-
return this.todos ? this.todos.filter(todo => !todo.completed).length : 0
39+
},
40+
removeCompleted: function () {
41+
const completedTodos = this.todos.filter(todo => todo.completed)
42+
completedTodos.forEach(todo => this.todos.splice(this.todos.indexOf(todo), 1))
4443
}
4544
},
4645
// watch todos change for localStorage persistence

lesson/03.vue-todos/src/components/Footer/Footer.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<li><router-link to="/Active" active-class="selected" exact replace>Active</router-link></li>
1010
<li><router-link to="/Completed" active-class="selected" exact replace>Completed</router-link></li>
1111
</ul>
12-
<button class="clear-completed">
12+
<button class="clear-completed" @click="removeCompleted" v-show="todos.length > remaining">
1313
Clear completed
1414
</button>
1515
</footer>
@@ -19,7 +19,17 @@
1919
export default {
2020
name: 'footer',
2121
// 声明 props
22-
props: ['remaining'],
22+
props: ['todos'],
23+
computed: {
24+
remaining: function () {
25+
return this.todos ? this.todos.filter(todo => !todo.completed).length : 0
26+
}
27+
},
28+
methods: {
29+
removeCompleted: function () {
30+
this.$emit('removeCompletedHandle')
31+
}
32+
},
2333
filters: {
2434
pluralize: function (n) {
2535
return n === 1 ? 'item' : 'items'

0 commit comments

Comments
 (0)