繼續看 Composition API 的 Reactive and toRefs。
Reactive
我們看上次的 ref 範例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <template> <div> <p>Space Left: {{ spaceLeft }} out of {{ capacity }}</p> <h2>Attending</h2> <ul> <li v-for="(name, index) in attending" :key="index"> {{ name }} </li> </ul> <button @click="increaseCapacity()">Increase Capacity</button> </div> </template> <script> import { ref, computed } from "vue"; export default { setup() { const capacity = ref(4); const attending = ref(["Tim", "Bob", "Joe"])
const spaceLeft = computed(() => { return capacity.value - attending.value.length; });
function increaseCapacity() { capacity.value++; }
return { capacity, attending, spaceLeft, increaseCapacity }; } }; </script>
|
我們可以用 Reactive 把全部的 ref 統整成一個物件 event。
下面的程式跟上面運行結果是一樣的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <template> <div> <p>Space Left: {{ event.spaceLeft }} out of {{ event.capacity }}</p> <h2>Attending</h2> <ul> <li v-for="(name, index) in event.attending" :key="index"> {{ name }} </li> </ul> <button @click="increaseCapacity()">Increase Capacity</button> </div> </template> <script> import { reactive, computed } from "vue"; export default { setup() { const event = reactive({ capacity: 4, attending: ["Tim", "Bob", "Joe"], spaceLeft: computed(() => { return event.capacity - event.attending.length; }); });
function increaseCapacity() { event.capacity++; }
return { event, increaseCapacity }; } }; </script>
|
HTML 裡面每次都要使用 event.xxx 感覺很煩,在這裡我們就可以用 toRefs ,再把他拆開成個別物件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <template> <div> <p>Space Left: {{ spaceLeft }} out of {{ capacity }}</p> <h2>Attending</h2> <ul> <li v-for="(name, index) in attending" :key="index"> {{ name }} </li> </ul> <button @click="increaseCapacity()">Increase Capacity</button> </div> </template> <script> import { reactive, computed, toRefs } from "vue"; export default { setup() { const event = reactive({ capacity: 4, attending: ["Tim", "Bob", "Joe"], spaceLeft: computed(() => { return event.capacity - event.attending.length; }); });
function increaseCapacity() { event.capacity++; }
return { toRefs(event), increaseCapacity }; } }; </script>
|
參考資料