App.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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="addTodo">
  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. const filters = {
  49. all: (todos) => todos,
  50. active: (todos) => todos.filter(todo => !todo.done),
  51. completed: (todos) => todos.filter(todo => todo.done)
  52. }
  53. export default {
  54. components: { Todo },
  55. data () {
  56. return {
  57. visibility: 'all',
  58. filters: filters
  59. }
  60. },
  61. computed: {
  62. todos () {
  63. return this.$store.state.todos
  64. },
  65. allChecked () {
  66. return this.todos.every(todo => todo.done)
  67. },
  68. filteredTodos () {
  69. return filters[this.visibility](this.todos)
  70. },
  71. remaining () {
  72. return this.todos.filter(todo => !todo.done).length
  73. }
  74. },
  75. methods: {
  76. addTodo (e) {
  77. var text = e.target.value
  78. if (text.trim()) {
  79. this.$store.actions.addTodo(text)
  80. }
  81. e.target.value = ''
  82. },
  83. toggleAll () {
  84. this.$store.actions.toggleAll()
  85. },
  86. clearCompleted () {
  87. this.$store.actions.clearCompleted()
  88. }
  89. }
  90. }
  91. </script>