今年四月信箱發現 Vue Mastery 又開放幾天全部課程免費的消息,所以趕快把之前沒看到的 Vue 3 Composition API 給補看起來!!

Methods

假設我們已經有一個 capacity 是 Reative Reference。現在要加 Methods 讓他每按一次加1。

1
2
3
4
5
6
7
8
9
10
11
12
<template>
<div>Capacity: {{ capacity }}</div>
</template>
<script>
import { ref } from "vue";
export default {
setup() {
const capacity = ref(3);
return { capacity };
}
};
</script>

原本 Vue 2 的寫法是

1
2
3
4
5
methods: {
increase_capacity() {
this.capacity++;
}
}

那在 Vue 3 只要把他想像成一般的 Function 再 return 出來即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
<div>
<p>Capacity: {{ capacity }}</p>
<button @click="increaseCapacity()">Increase Capacity</button>
</div>
</template>
<script>
import { ref } from "vue";
export default {
setup() {
const capacity = ref(3);

function increaseCapacity() {
capacity.value++;
}

return { capacity, increaseCapacity };
}
};
</script>

注意!capacity 是 Reactive Reference,是一個物件封裝他,所以對物件做增加的動作是不管用的。我們需要針對裡面的 value 增加才行。所以這邊是對 capacity.value 做增加的動作。

Methods example demo

Computed

我們繼續用上面的例子加上一組人名陣列清單,並用 computed 計算剩下的容量有多少。

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
<template>
<div>
<p>Capacity: {{ 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 } from "vue";
export default {
setup() {
const capacity = ref(3);
const attending = ref(["Tim", "Bob", "Joe"])

function increaseCapacity() {
capacity.value++;
}

return { capacity, attending, increaseCapacity };
}
};
</script>

如果是 Vue 2 的寫法是

1
2
3
4
5
computed: {
spaceLeft() {
return this.capacity - this.attending.length;
}
}

在 Vue 3 則是呼叫 computed 函數即可。

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(3);
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>

Computed example demo

參考資料