Vue-Composition API(Life Cycle Hooks)

又見面了。

簡述

setup 中也可以使用 Lifecycle hooks,唯一的差別就是多了 on- 這個前綴,例如 mounted -> onMounted

這邊附贈一個簡單的範例。

Home.vue

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
<template>
<div class="home">
<!-- 觸發 onUnmounted -->
<router-link :to="{ name: 'About' }">About</router-link>
<!-- 輸入時觸發 onUpdated -->
<input type="text" v-model="input" />
</div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { onMounted, onUnmounted, onUpdated } from '@vue/runtime-core'

export default {
setup() {
onMounted(() => console.log('mounted'))
onUpdated(() => console.log('updated'))
onUnmounted(() => console.log('unmounted'))

const input = ref('')

return { input }
}
}
</script>

應該不難理解,真的有疑問的話到 這邊 玩玩看吧。

Vue 版的 Custom Hook-Composables Vue-Composition API(Props)
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×