1
0

App.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <style src="todomvc-app-css/index.css"></style>
  2. <template>
  3. <section class="todoapp">
  4. <!-- header -->
  5. <header class="header">
  6. <h1>todos</h1>
  7. <input class="new-todo"
  8. autofocus
  9. autocomplete="off"
  10. placeholder="What needs to be done?"
  11. @keyup.enter="tryAddTodo">
  12. </header>
  13. <!-- main section -->
  14. <section class="main" v-show="todos.length">
  15. <input class="toggle-all"
  16. type="checkbox"
  17. :checked="allChecked"
  18. @change="toggleAll(!allChecked)">
  19. <ul class="todo-list">
  20. <todo v-for="todo in filteredTodos" :todo="todo"></todo>
  21. </ul>
  22. </section>
  23. <!-- footer -->
  24. <footer class="footer" v-show="todos.length">
  25. <span class="todo-count">
  26. <strong>{{ remaining }}</strong>
  27. {{ remaining | pluralize 'item' }} left
  28. </span>
  29. <ul class="filters">
  30. <li v-for="(key, val) in filters">
  31. <a href="#/{{$key}}"
  32. :class="{ selected: visibility === key }"
  33. @click="visibility = key">
  34. {{ key | capitalize }}
  35. </a>
  36. </li>
  37. </ul>
  38. <button class="clear-completed"
  39. v-show="todos.length > remaining"
  40. @click="clearCompleted">
  41. Clear completed
  42. </button>
  43. </footer>
  44. </section>
  45. </template>
  46. <script>
  47. import Todo from './Todo.vue'
  48. import {
  49. addTodo,
  50. toggleAll,
  51. clearCompleted
  52. } from '../vuex/actions'
  53. const filters = {
  54. all: todos => todos,
  55. active: todos => todos.filter(todo => !todo.done),
  56. completed: todos => todos.filter(todo => todo.done)
  57. }
  58. export default {
  59. components: { Todo },
  60. vuex: {
  61. state: {
  62. todos: state => state.todos
  63. },
  64. actions: {
  65. addTodo,
  66. toggleAll,
  67. clearCompleted
  68. }
  69. },
  70. data () {
  71. return {
  72. visibility: 'all',
  73. filters: filters
  74. }
  75. },
  76. computed: {
  77. allChecked () {
  78. return this.todos.every(todo => todo.done)
  79. },
  80. filteredTodos () {
  81. return filters[this.visibility](this.todos)
  82. },
  83. remaining () {
  84. return this.todos.filter(todo => !todo.done).length
  85. }
  86. },
  87. methods: {
  88. tryAddTodo (e) {
  89. var text = e.target.value
  90. if (text.trim()) {
  91. this.addTodo(text)
  92. }
  93. e.target.value = ''
  94. }
  95. }
  96. }
  97. </script>