public ----> index.css
/*base*/
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 0 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;
}
public ----> index.html
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<link rel="stylesheet" href="./index.css">
<title>
<%= htmlWebpackPlugin.options.title %>
</title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
main.js
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this
}
}).$mount('#app')
App.vue
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<Header></Header>
<Main :todos="todos"></Main>
<Footer :todos="todos"></Footer>
</div>
</div>
</div>
</template>
<script>
import Header from './components/Heater/index.vue'
import Main from './components/Mian/index.vue'
import Footer from './components/Footer/index.vue'
export default {
data() {
return {
todos: JSON.parse(localStorage.getItem('todo')) || [
{ id: 1, title: '吃饭', done: true },
{ id: 2, title: '睡觉', done: false },
{ id: 3, title: '敲代码', done: true }
]
}
},
watch: {
todos: {
handler() {
localStorage.setItem('todo', JSON.stringify(this.todos))
},
deep: true
}
},
methods: {
// 添加内容
addtodos(value) {
if (value.trim().length < 0) {
alert('不要添加空内容')
return
}
this.todos.unshift({ id: Date.now(), title: value, done: false })
},
// 删除内容
removeTodo(id) {
return (this.todos = this.todos.filter(item => item.id !== id))
},
// 修改内容
reviseTodo(id) {
this.todos.map(item => {
if (item.id === id) {
item.done = !item.done
}
})
},
// 全选or反选
AllTodoDone(val) {
this.todos.forEach(item => (item.done = val))
},
// 删除已完成
doneTodo() {
return (this.todos = this.todos.filter(item => !item.done))
}
},
mounted() {
// 添加内容
this.$bus.$on('addTodo', this.addtodos)
// 删除内容
this.$bus.$on('deleteTodo', this.removeTodo)
// 修改内容
this.$bus.$on('reviseTodo', this.reviseTodo)
// 全选反选
this.$bus.$on('allTodo', this.AllTodoDone)
// 删除已完成
this.$bus.$on('doneTodo', this.doneTodo)
},
components: {
Header,
Main,
Footer
}
}
</script>
<style>
/*app*/
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>
components ----> Header/index.vue
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="todoText" @keyup.enter="addtodosText" />
</div>
</template>
<script>
export default {
data() {
return {
todoText: ''
}
},
methods: {
addtodosText() {
this.$bus.$emit('addTodo', this.todoText)
this.todoText = ''
}
}
}
</script>
<style>
/*header*/
.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 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>
components ----> Main/index.vue
<template>
<ul class="todo-main">
<Item v-for="todo in todos" :key="todo.id" :todo="todo"></Item>
</ul>
</template>
<script>
import Item from '../Item/index.vue'
export default {
props: {
todos: {
type: Array,
require: true
}
},
components: {
Item
}
}
</script>
<style scoped>
/*main*/
.todo-main {
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>
components ----> Item/index.vue
<template>
<li @mouseenter="isShow=true" @mouseleave="isShow=false" :class="{active:isShow}">
<label>
<input type="checkbox" :checked="todo.done" @change="changeChecked(todo.id)" />
<span>{{todo.title}}</span>
</label>
<button class="btn btn-danger" v-show='isShow' @click="deleteTodo(todo.id)">删除</button>
</li>
</template>
<script>
export default {
data() {
return {
isShow: false
}
},
props: {
todo: {
type: Object,
require: true
}
},
methods: {
deleteTodo(id) {
this.$bus.$emit('deleteTodo', id)
},
changeChecked(id) {
this.$bus.$emit('reviseTodo', id)
}
}
}
</script>
<style scoped>
.active {
background-color: pink;
}
/*item*/
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 li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
</style>
components ----> Footer/index.vue
<template>
<div class="todo-footer">
<label>
<input type="checkbox" v-model="AllTodo" />
</label>
<span>
<span>已完成{{doneTodo}}</span> / 全部{{todos.length}}
</span>
<button class="btn btn-danger" @click="removeDone">清除已完成任务</button>
</div>
</template>
<script>
export default {
props: {
todos: {
type: Array,
require: true
}
},
computed: {
doneTodo() {
return this.todos.reduce((pre, item) => {
return pre + item.done
}, 0)
},
AllTodo: {
get() {
return this.todos.every(item => item.done)
},
set(value) {
this.$bus.$emit('allTodo', value)
}
}
},
methods: {
removeDone() {
this.$bus.$emit('doneTodo')
}
}
}
</script>
<style>
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
</style>