事件监听绑定、触发的另一种形式$on(eventName,data)
App.vue组件
<template>
<div class="todo-container">
<div class="todo-wrap">
<!--<TodoHeader @addTodo="addTodo"/>-->
<TodoHeader ref="header"/>
<TodoList :todos="todos"/>
<TodoFooter :todos="todos" :deleteCompleteTodos="deleteCompleteTodos" :selectAll="selectAll"/>
</div>
</div>
</template>
<script>
import PubSub from 'pubsub-js'
import TodoHeader from './components/TodoHeader.vue'
import TodoList from './components/TodoList.vue'
import TodoFooter from './components/TodoFooter.vue'
import storageUtils from './utils/storageUtils'
export default {
data () {
return {
// todos: JSON.parse(localStorage.getItem('todos_key') || '[]') // 读取localStorage保存的数据
todos: storageUtils.readTodos()
}
},
mounted () { //执行异步代码
// 给<TodoHeader/>绑定自定义事件(addTodo)监听
// this.$on('addTodo', this.addTodo) 绑定监听的目标不对,这里的this是App组件,而addTodo绑定在TodoHeader上,需要TodoHeader去触发
this.$refs.header.$on('addTodo', this.addTodo)
// 订阅消息(deleteTodo)
PubSub.subscribe('deleteTodo', (msg, index) => {
this.deleteTodo(index)
})
},
methods: {
addTodo (todo) {
this.todos.unshift(todo)
},
deleteTodo (index) {
this.todos.splice(index, 1)
},
// 删除所有已完成的
deleteCompleteTodos () {
this.todos = this.todos.filter(todo => !todo.complete)
},
// 全选/全不选
selectAll (isSelectAll) {
this.todos.forEach(todo => {
todo.complete = isSelectAll
})
}
},
watch: {
todos: {
deep: true, // 深度监视
/*handler: function (val) {
// 将数据(json)保存到localStorage
// localStorage.setItem('todos_key', JSON.stringify(val))
storageUtils.saveTodos(val)
}*/
// handler的值应该是一个函数, 且函数应该要有一个形参(接收todos最新的值)
handler: storageUtils.saveTodos,
/*handler: function (todos) {
localStorage.setItem(TODOS_KEY, JSON.stringify(todos))
}*/
}
},
components: {
TodoHeader,
TodoList,
TodoFooter
}
}
</script>
<style>
</style>