介绍
vue3的生命周期函数,可以按需导入到组件中,且只能在 setup() 函数中使用
示例
import { onMounted, onUpdated } from '@vue/composition-api'
export default {
setup() {
onMounted(() => {
console.log('mounted')
})
onUpdated(() => {
console.log('updated')
})
}
}
对比vue2.x
-
-> 使用beforeCreatesetup() -
-> 使用createdsetup() -
beforeMount->onBeforeMount -
mounted->onMounted -
beforeUpdate->onBeforeUpdate -
updated->onUpdated -
beforeDestroy->onBeforeUnmount -
destroyed->onUnmounted -
errorCaptured->onErrorCaptured