App.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <div id="root">
  3. <div class="todo-container">
  4. <div class="todo-wrap">
  5. <MyHeader @addTodo="addTodo"></MyHeader>
  6. <MyList :todos="todos"></MyList>
  7. <MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @cleatAllTodo="cleatAllTodo"></MyFooter>
  8. </div>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import pubsub from 'pubsub-js'
  14. import MyHeader from "./components/MyHeader.vue";
  15. import MyList from "./components/MyList.vue";
  16. import MyFooter from "./components/MyFooter.vue";
  17. export default {
  18. name: "App",
  19. components: { MyHeader, MyList, MyFooter },
  20. data() {
  21. return {
  22. todos: JSON.parse(localStorage.getItem('todos')) || []
  23. };
  24. },
  25. methods:{
  26. //添加一个todo
  27. addTodo(todoObj){
  28. // console.log('我是App组件,我收到了数据:',todoObj)
  29. this.todos.unshift(todoObj)
  30. },
  31. //勾选或取消勾选一个todo
  32. checkTodo(id){
  33. this.todos.forEach((todo) => {
  34. if(todo.id === id) todo.done = !todo.done
  35. })
  36. },
  37. //更新一个todo
  38. updateTodo(id,title){
  39. this.todos.forEach((todo) => {
  40. if(todo.id === id) todo.title = title
  41. })
  42. },
  43. //删除一个todo
  44. deleteTodo(_,id){
  45. //使用了消息订阅与发布,当deleteTodo作为消息的回调使用时,会接收到两个参数,
  46. //第一个参数是msgName,由于本例用不上,所以使用下划线_,表示占位
  47. // const todos = this.todos.filter((todo) => {
  48. // return todo.id !== id
  49. // })
  50. //简化写法
  51. this.todos = this.todos.filter(todo => todo.id !== id)
  52. },
  53. //全选或取消全选
  54. checkAllTodo(done){
  55. this.todos.forEach((todo) => {
  56. todo.done = done
  57. })
  58. },
  59. //清除已经完成的todo
  60. cleatAllTodo(){
  61. this.todos = this.todos.filter((todo) => {
  62. return !todo.done
  63. })
  64. }
  65. },
  66. // 实现本地存储
  67. watch:{
  68. todos:{
  69. deep:true,
  70. handler(value){
  71. localStorage.setItem('todos',JSON.stringify(value))
  72. }
  73. }
  74. },
  75. mounted(){
  76. this.$bus.$on('checkTodo',this.checkTodo)
  77. // this.$bus.$on('deleteTodo',this.deleteTodo)
  78. this.pubId = pubsub.subscribe('deleteTodo',this.deleteTodo)
  79. this.$bus.$on('updateTodo',this.updateTodo)
  80. },
  81. beforeDestroy(){
  82. this.$bus.$off('checkTodo')
  83. // this.$bus.$off('deleteTodo')
  84. pubsub.unsubscribe(this.pubId)
  85. this.$bus.$off('updateTodo')
  86. }
  87. };
  88. </script>
  89. <style>
  90. /*base*/
  91. body {
  92. background: #fff;
  93. }
  94. .btn {
  95. display: inline-block;
  96. padding: 4px 12px;
  97. margin-bottom: 0;
  98. font-size: 14px;
  99. line-height: 20px;
  100. text-align: center;
  101. vertical-align: middle;
  102. cursor: pointer;
  103. box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
  104. 0 1px 2px rgba(0, 0, 0, 0.05);
  105. border-radius: 4px;
  106. }
  107. .btn-danger {
  108. color: #fff;
  109. background-color: #da4f49;
  110. border: 1px solid #bd362f;
  111. }
  112. .btn-edit {
  113. color: #fff;
  114. background-color: skyblue;
  115. border: 1px solid rgb(108, 201, 238);
  116. margin-right: 5px;
  117. }
  118. .btn-danger:hover {
  119. color: #fff;
  120. background-color: #bd362f;
  121. }
  122. .btn:focus {
  123. outline: none;
  124. }
  125. .todo-container {
  126. width: 600px;
  127. margin: 0 auto;
  128. }
  129. .todo-container .todo-wrap {
  130. padding: 10px;
  131. border: 1px solid #ddd;
  132. border-radius: 5px;
  133. }
  134. </style>