todolist项目中实践自定义组件

new_branch1
roger 2 years ago
parent 590facbd52
commit 833a8c3a6e
  1. 118
      20_脚手架/vue_code/10.src_todolist自定义事件/App.vue
  2. BIN
      20_脚手架/vue_code/10.src_todolist自定义事件/assets/logo.png
  3. 76
      20_脚手架/vue_code/10.src_todolist自定义事件/components/TodoBottom.vue
  4. 49
      20_脚手架/vue_code/10.src_todolist自定义事件/components/TodoHeader.vue
  5. 74
      20_脚手架/vue_code/10.src_todolist自定义事件/components/TodoItem.vue
  6. 41
      20_脚手架/vue_code/10.src_todolist自定义事件/components/TodoList.vue
  7. 16
      20_脚手架/vue_code/10.src_todolist自定义事件/main.js
  8. 128
      20_脚手架/vue_code/src/App.vue
  9. 35
      20_脚手架/vue_code/src/components/School.vue
  10. 52
      20_脚手架/vue_code/src/components/Student.vue

@ -0,0 +1,118 @@
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<TodoHeader @addTodo="addTodo"/>
<TodoList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
<TodoBottom :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/>
</div>
</div>
</div>
</template>
<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoList from "./components/TodoList.vue";
import TodoBottom from "./components/TodoBottom.vue";
export default {
name: "App",
components: {
TodoHeader,
TodoList,
TodoBottom
},
data() {
return {
//
todos: JSON.parse(localStorage.getItem('todos')) || []
}
},
// 访App
// App
methods: {
// todoTodoHeader
addTodo(todoObj) {
this.todos.unshift(todoObj)
},
// todo穿TodoListTodoItem
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done
})
},
// todo穿TodoListTodoItem
deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id)
},
// todoTodoBottom
checkAllTodo(done) {
this.todos.forEach((todo) => todo.done = done)
},
// todoTodoBottom
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done
})
}
},
watch: {
todos: {
deep: true,
handler(value) {
localStorage.setItem('todos', JSON.stringify(value))
}
}
}
}
</script>
<style>
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,76 @@
<template>
<div class="todo-bottom" v-show="total">
<label>
<input type="checkbox" v-model="isAll">
</label>
<span>
<span>已完成{{ totalDone }}</span>/全部{{ total }}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "TodoBottom",
// props: ['todos', 'checkAllTodo', 'clearAllTodo'],
props: ['todos'],
computed: {
// todov-show
total() {
return this.todos.length
},
// todo
totalDone() {
return this.todos.reduce((pre, todo) => pre + (todo.done ? 1 : 0), 0)
},
//
// get
// setAppcheckAllTodo
isAll: {
get() {
return this.totalDone === this.total && this.total > 0
},
set(value) {
// this.checkAllTodo(value)
this.$emit('checkAllTodo', value)
}
}
},
methods: {
// AppclearAllTodo
clearAll() {
if (confirm("确认清除全部已完成任务?")) {
this.$emit('clearAllTodo')
}
}
}
}
</script>
<style scoped>
.todo-bottom {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-bottom label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-bottom label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-bottom button {
float: right;
margin-top: 5px;
}
</style>

@ -0,0 +1,49 @@
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车确认" v-model="title" @keyup.en.enter="add">
</div>
</template>
<script>
import {nanoid} from "nanoid"
export default {
name: "TodoHeader",
// props: ['addTodo'],
data() {
// titleplaceholder
return {
title: ''
}
},
methods: {
// todotrim
// Apptodo
add() {
if (!this.title.trim()) return alert("输入不能为空")
const todoObj = {id: nanoid(), title: this.title, done: false}
// this.addTodo(todoObj) // emit
this.$emit('addTodo', todoObj)
this.title = ''
}
}
}
</script>
<style scoped>
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>

@ -0,0 +1,74 @@
<template>
<li>
<label>
<input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)">
<span>{{ todo.title }}</span>
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
</li>
</template>
<script>
export default {
name: "TodoItem",
// todoListtodoscheckTododeleteTodo
props: ['todo', 'checkTodo', 'deleteTodo'],
methods: {
// checkTodoTodoItem
handleCheck(id) {
this.checkTodo(id)
},
// deleteTodoTodoItem
handleDelete(id) {
if (confirm("确认删除?")) {
this.deleteTodo(id)
}
}
}
}
</script>
<style scoped>
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
}
li label {
float: left;
cursor: pointer;
}
li label input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
display: none;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
li:hover {
background-color: #ddd;
}
li:hover button {
display: block;
}
</style>

@ -0,0 +1,41 @@
<template>
<ul class="todo-list">
<TodoItem
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
:checkTodo="checkTodo"
:deleteTodo="deleteTodo"
/>
</ul>
</template>
<script>
import TodoItem from "@/components/TodoItem.vue";
export default {
name: "TodoList",
// TodoItem
components: {TodoItem},
// ApptodoscheckTododeleteTodotodoItembypass
props: ['todos', 'checkTodo', 'deleteTodo']
}
</script>
<style scoped>
.todo-list {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
</style>

@ -0,0 +1,16 @@
// 引入Vue
import Vue from "vue";
// 引入App
import App from "./App";
// 设置Vue
Vue.config.productionTip = false
// 实例化Vue
new Vue({
components: {
App
},
render: h => h(App)
}).$mount('#app')

@ -1,64 +1,118 @@
<template> <template>
<div class="app"> <div id="root">
<h1>{{ msg }}{{stuName}}</h1> <div class="todo-container">
<!-- 通过父组件给子组件传递函数间接获取子组件数据 --> <div class="todo-wrap">
<School :getSchoolName="getSchoolName"/> <TodoHeader @addTodo="addTodo"/>
<!-- 通过父组件给子组件绑定一个自定义事件实现给父传递数据使用v-on或@方式 --> <TodoList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
<!-- <Student v-on:studentName="getStudentName"/>--> <TodoBottom :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/>
<Student @studentName="getStudentName" @demo="method1" @click.native="show"/> </div>
<!-- <Student @studentName.once="getStudentName"/>--> </div>
<!-- 通过父组件给子组件绑定一个自定义事件实现给父传递数据,使用ref方式 -->
<!-- <Student ref="student"/>-->
</div> </div>
</template> </template>
<script> <script>
import School from "./components/School.vue"; import TodoHeader from "./components/TodoHeader.vue";
import Student from "./components/Student.vue"; import TodoList from "./components/TodoList.vue";
import TodoBottom from "./components/TodoBottom.vue";
export default { export default {
name: "App", name: "App",
components: { components: {
School, TodoHeader,
Student TodoList,
TodoBottom
}, },
data() { data() {
return { return {
msg: "Hello:", //
stuName: "" todos: JSON.parse(localStorage.getItem('todos')) || []
} }
}, },
// 访App
// App
methods: { methods: {
getSchoolName(schoolName) { // todoTodoHeader
console.log("获取学校名称", schoolName) addTodo(todoObj) {
this.todos.unshift(todoObj)
}, },
getStudentName(name, ...params) {
console.log("getStudentName", name, params) // todo穿TodoListTodoItem
this.stuName = name checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done
})
}, },
method1() {
console.log("demo自定义事件被调用!") // todo穿TodoListTodoItem
deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id)
}, },
show() {
alert("组件点击事件") // todoTodoBottom
checkAllTodo(done) {
this.todos.forEach((todo) => todo.done = done)
},
// todoTodoBottom
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done
})
} }
}, },
mounted() { watch: {
// todos: {
// this.$refs.student.$on("studentName", this.getStudentName) deep: true,
// handler(value) {
// localStorage.setItem('todos', JSON.stringify(value))
// this.$refs.student.$once("studentName", this.getStudentName) }
// setInterval(() => { }
// this.$refs.student.$on("studentName", this.getStudentName)
// }, 3000)
} }
} }
</script> </script>
<style> <style>
.app { body {
background-color: gray; background: #fff;
padding: 15px }
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
} }
</style> </style>

@ -1,35 +0,0 @@
<template>
<div class="school">
<h2>学校名称{{ name }}</h2>
<h2>学校地址{{ address }}</h2>
<!-- 通过父组件给子组件传递函数间接获取子组件数据 -->
<button @click="sendSchoolName">把学校名称传给App</button>
</div>
</template>
<script>
export default {
name: "School",
props: ["getSchoolName"],
data() {
return {
name: "修仙学院",
address: "长白山",
}
},
methods: {
sendSchoolName() {
this.getSchoolName(this.name)
}
}
}
</script>
<style scoped>
.school {
background-color: skyblue;
padding: 15px
}
</style>

@ -1,52 +0,0 @@
<template>
<div class="student">
<h2>学生名称{{ name }}</h2>
<h2>学生年龄{{ age }}</h2>
<h2>当前序号{{number}}</h2>
<button @click="sendStudentName">把学生名称传给App</button>
<button @click="unbind">解绑studentName事件</button>
<button @click="death">销毁当前Student组件的实例</button>
<button @click="num">number+1</button>
</div>
</template>
<script>
export default {
name: "Student",
data() {
return {
name: "小强",
age: 18,
number: 0
}
},
methods: {
sendStudentName() {
// StudentstudentName
this.$emit("studentName", this.name, 666, 777, 888)
this.$emit("demo")
},
unbind() {
// this.$off("studentName") //
this.$off(["studentName", "demo"]) //
this.$off() //
},
death() {
this.$destroy() // Student
},
num() {
console.log("num被调用")
this.number++
}
}
}
</script>
<style lang="css" scoped>
.student {
background-color: pink;
padding: 15px;
margin-top: 20px;
}
</style>
Loading…
Cancel
Save