diff --git a/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/App.vue b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/App.vue new file mode 100644 index 0000000..ddfd312 --- /dev/null +++ b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/App.vue @@ -0,0 +1,145 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/assets/logo.png b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/assets/logo.png new file mode 100644 index 0000000..f3d2503 Binary files /dev/null and b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/assets/logo.png differ diff --git a/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoBottom.vue b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoBottom.vue new file mode 100644 index 0000000..b4de571 --- /dev/null +++ b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoBottom.vue @@ -0,0 +1,76 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoHeader.vue b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoHeader.vue new file mode 100644 index 0000000..d4a857c --- /dev/null +++ b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoHeader.vue @@ -0,0 +1,49 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoItem.vue b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoItem.vue new file mode 100644 index 0000000..027b8fe --- /dev/null +++ b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoItem.vue @@ -0,0 +1,92 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoList.vue b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoList.vue new file mode 100644 index 0000000..4be4cc2 --- /dev/null +++ b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/components/TodoList.vue @@ -0,0 +1,38 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/main.js b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/main.js new file mode 100644 index 0000000..4d5e394 --- /dev/null +++ b/20_脚手架/vue_code/15.src_todolist添加编辑逻辑/main.js @@ -0,0 +1,19 @@ +// 引入Vue +import Vue from "vue"; +// 引入App +import App from "./App"; + +// 设置Vue +Vue.config.productionTip = false + + +// 实例化Vue +new Vue({ + components: { + App + }, + render: h => h(App), + beforeCreate() { + Vue.prototype.$bus = this // 安装全局事件总线 + } +}).$mount('#app') diff --git a/20_脚手架/vue_code/src/App.vue b/20_脚手架/vue_code/src/App.vue index 2bdf923..ddfd312 100644 --- a/20_脚手架/vue_code/src/App.vue +++ b/20_脚手架/vue_code/src/App.vue @@ -50,6 +50,13 @@ export default { this.todos = this.todos.filter(todo => todo.id !== id) }, + // 更新一个TodoItem + updateTodo(id, title) { + this.todos.forEach((todo) => { + if (todo.id === id) todo.title = title + }) + }, + // 勾选或取消勾选全部todo任务,绑定到TodoBottom中 checkAllTodo(done) { this.todos.forEach((todo) => todo.done = done) @@ -73,13 +80,13 @@ export default { // 生成全局事件总线 mounted() { this.$bus.$on('checkTodo', this.checkTodo) - // this.$bus.$on('deleteTodo', this.deleteTodo) + this.$bus.$on('updateTodo', this.updateTodo) this.pubId = pubsub.subscribe('itemDel', this.deleteTodo) // 使用订阅方式实现删除功能 }, // 销毁全局事件总线 beforeDestroy() { this.$bus.$off('checkTodo') - // this.$bus.$off('deleteTodo') + this.$bus.$off('updateTodo') this.pubsub.unsubscribe(this, pubId) // 使用订阅方式实现删除功能,销毁订阅 } } @@ -109,6 +116,13 @@ body { border: 1px solid #bd362f; } +.btn-edit { + color: #fff; + background-color: skyblue; + border: 1px solid #82cdea; + margin-right: 5px; +} + .btn-danger:hover { color: #fff; background-color: #bd362f; diff --git a/20_脚手架/vue_code/src/components/TodoItem.vue b/20_脚手架/vue_code/src/components/TodoItem.vue index 8aa3ac4..027b8fe 100644 --- a/20_脚手架/vue_code/src/components/TodoItem.vue +++ b/20_脚手架/vue_code/src/components/TodoItem.vue @@ -2,9 +2,11 @@
  • +
  • @@ -23,8 +25,23 @@ export default { // 通过订阅消息形式实现 handleDelete(id) { if (confirm("确认删除?")) { - pubsub.publish('itemDel',id) + pubsub.publish('itemDel', id) } + }, + // 实现编辑逻辑 + handleEdit(todo) { + if (todo.hasOwnProperty('isEdit')) { + todo.isEdit = true + }else { + this.$set(todo, 'isEdit', true) + } + }, + // 失去焦点时更新数据 + handleBlur(todo, e) { + todo.isEdit = false + // console.log('updateTodo', todo.id, e.target.value) + if (e.target.value.trim()) return alert("输入不能为空!") + this.$bus.$emit('updateTodo', todo.id, e.target.value) } } }