App.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. deleteTodo(_,id){
  39. //使用了消息订阅与发布,当deleteTodo作为消息的回调使用时,会接收到两个参数,
  40. //第一个参数是msgName,由于本例用不上,所以使用下划线_,表示占位
  41. // const todos = this.todos.filter((todo) => {
  42. // return todo.id !== id
  43. // })
  44. //简化写法
  45. this.todos = this.todos.filter(todo => todo.id !== id)
  46. },
  47. //全选或取消全选
  48. checkAllTodo(done){
  49. this.todos.forEach((todo) => {
  50. todo.done = done
  51. })
  52. },
  53. //清除已经完成的todo
  54. cleatAllTodo(){
  55. this.todos = this.todos.filter((todo) => {
  56. return !todo.done
  57. })
  58. }
  59. },
  60. // 实现本地存储
  61. watch:{
  62. todos:{
  63. deep:true,
  64. handler(value){
  65. localStorage.setItem('todos',JSON.stringify(value))
  66. }
  67. }
  68. },
  69. mounted(){
  70. this.$bus.$on('checkTodo',this.checkTodo)
  71. // this.$bus.$on('deleteTodo',this.deleteTodo)
  72. this.pubId = pubsub.subscribe('deleteTodo',this.deleteTodo)
  73. },
  74. beforeDestroy(){
  75. this.$bus.$off('checkTodo')
  76. // this.$bus.$off('deleteTodo')
  77. pubsub.unsubscribe(this.pubId)
  78. }
  79. };
  80. </script>
  81. <style scoped>
  82. /*base*/
  83. body {
  84. background: #fff;
  85. }
  86. .btn {
  87. display: inline-block;
  88. padding: 4px 12px;
  89. margin-bottom: 0;
  90. font-size: 14px;
  91. line-height: 20px;
  92. text-align: center;
  93. vertical-align: middle;
  94. cursor: pointer;
  95. box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
  96. 0 1px 2px rgba(0, 0, 0, 0.05);
  97. border-radius: 4px;
  98. }
  99. .btn-danger {
  100. color: #fff;
  101. background-color: #da4f49;
  102. border: 1px solid #bd362f;
  103. }
  104. .btn-danger:hover {
  105. color: #fff;
  106. background-color: #bd362f;
  107. }
  108. .btn:focus {
  109. outline: none;
  110. }
  111. .todo-container {
  112. width: 600px;
  113. margin: 0 auto;
  114. }
  115. .todo-container .todo-wrap {
  116. padding: 10px;
  117. border: 1px solid #ddd;
  118. border-radius: 5px;
  119. }
  120. </style>