|
1 | 1 | <template> |
2 | | - <div id="app"></div> |
| 2 | + <div id="app"> |
| 3 | + <section class="todoapp"> |
| 4 | + <header class="header"> |
| 5 | + <h1>todos</h1> |
| 6 | + <input |
| 7 | + class="new-todo" |
| 8 | + autofocus="" |
| 9 | + autocomplete="off" |
| 10 | + placeholder="What needs to be done?" |
| 11 | + /> |
| 12 | + </header> |
| 13 | + <section class="main"> |
| 14 | + <input id="toggle-all" class="toggle-all" type="checkbox" /> |
| 15 | + <label for="toggle-all">Mark all as complete</label> |
| 16 | + <ul class="todo-list"> |
| 17 | + <li class="todo"> |
| 18 | + <div class="view"> |
| 19 | + <input class="toggle" type="checkbox" /> |
| 20 | + <label>composition api demo</label> |
| 21 | + <button class="destroy"></button> |
| 22 | + </div> |
| 23 | + <input class="edit" type="text" /> |
| 24 | + </li> |
| 25 | + <li class="todo"> |
| 26 | + <div class="view"> |
| 27 | + <input class="toggle" type="checkbox" /> |
| 28 | + <label>写简历</label> |
| 29 | + <button class="destroy"></button> |
| 30 | + </div> |
| 31 | + <input class="edit" type="text" /> |
| 32 | + </li> |
| 33 | + <li class="todo"> |
| 34 | + <div class="view"> |
| 35 | + <input class="toggle" type="checkbox" /> |
| 36 | + <label>下午3点参加面试</label> |
| 37 | + <button class="destroy"></button> |
| 38 | + </div> |
| 39 | + <input class="edit" type="text" /> |
| 40 | + </li> |
| 41 | + </ul> |
| 42 | + </section> |
| 43 | + <footer class="footer"> |
| 44 | + <span class="todo-count"> |
| 45 | + <strong>3</strong> |
| 46 | + <span>items left</span> |
| 47 | + </span> |
| 48 | + <ul class="filters"> |
| 49 | + <li><a href="#/all" class="selected">All</a></li> |
| 50 | + <li><a href="#/active" class="">Active</a></li> |
| 51 | + <li><a href="#/completed" class="">Completed</a></li> |
| 52 | + </ul> |
| 53 | + <button class="clear-completed" style="display: none"> |
| 54 | + Clear completed |
| 55 | + </button> |
| 56 | + </footer> |
| 57 | + </section> |
| 58 | + </div> |
3 | 59 | </template> |
4 | 60 |
|
5 | 61 | <script> |
6 | | -export default {}; |
| 62 | +import { ref, watchEffect } from "vue"; |
| 63 | +import * as todoStorage from "./utils/todoStorage"; |
| 64 | +
|
| 65 | +export default { |
| 66 | + setup() { |
| 67 | + const todoRef = ref(todoStorage.fetchTodos()); |
| 68 | + // 监控副作用 |
| 69 | + // 在 watch 中读取的会自动收集依赖 |
| 70 | + // 当依赖变化时会自动运行该副作用函数 |
| 71 | + watchEffect(() => { |
| 72 | + todoStorage.saveTodos(todoRef.value); |
| 73 | + }); |
| 74 | + }, |
| 75 | +}; |
7 | 76 | </script> |
8 | 77 |
|
9 | 78 | <style></style> |
0 commit comments