App.vue 2.2 KB

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