关于vue.js:Vue3生命周期

vue3 生命周期钩子

官网文档

上面总结下几个罕用的生命周期钩子执行程序

一 单个组件生命周期钩子执行程序

<template>
   <div>{{num}}</div>
</template>

<script setup lang="ts">

import { 
    ref, watchEffect, 
    onBeforeMount, onMounted,
    onBeforeUpdate,onUpdated,
    onBeforeUnmount,onUnmounted
} from 'vue';

//--------------------------------
let num = ref(1);
setTimeout(() => {
    num.value += 1;
}, 500);
//--------------------------------
watchEffect(
    () => {
        let b = num.value;
        console.log('welcom组件--watchEffect');
    },
    {
        // flush: 'post'
        flush: 'pre' // 默认此配置
    }
);
//--------------------------------
onBeforeMount(() => {});

onMounted(() => {});

onBeforeUpdate(() => {});

onUpdated(() => {});

onBeforeUnmount(() => {});

onUnmounted(() => {});

</script>

二 父子组件生命周期钩子执行程序

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理