vue 子父组件周期顺序_父组件和子组件生命周期钩子执行顺序是什么?

加载页面执行步骤

1、父组件:beforeCreate -> created -> beforeMount

2、子组件:beforeCreate -> created -> beforeMount -> mounted

3、父组件:mounted

销毁组件执行步骤

1、父组件:beforeDestroy

2、子组件:beforeDestroy

3、子组件:destroyed

2、父组件:destroyed

验证效果(加载页面执行步骤

index.vue

<template>
    <!-- 首页 -->
    <div class="views__home">
        <sub-model></sub-model>
    </div>
</template>

<script>
import SubModel from './sub-model.vue'; // 子组件
export default {
    components: {
        SubModel
    },
    beforeCreate() {
        console.log('父组件', 'beforeCreate');
    },
    created() {
        console.log('父组件', 'created');
    },
    beforeMount() {
        console.log('父组件', 'beforeMount');
    },
    mounted() {
        console.log('父组件', 'mounted');
    }
};
</script>

<style lang="less" scoped></style>

sub-model.vue

<template>
    <!-- 子组件 -->
    <div class="views__home__sub-model">

    </div>
</template>

<script>
export default {
    beforeCreate() {
        console.log('子组件', 'beforeCreate');
    },
    created() {
        console.log('子组件', 'created');
    },
    beforeMount() {
        console.log('子组件', 'beforeMount');
    },
    mounted() {
        console.log('子组件', 'mounted');
    }
};
</script>

<style lang="less" scoped></style>

打印结果

abaa793f3a8e5f5fd80314eaa8e6aa1e.png

验证效果(销毁组件执行步骤

index.vue

<template>
    <!-- 首页 -->
    <div class="views__home">
        <sub-model></sub-model>
    </div>
</template>

<script>
import SubModel from './sub-model.vue'; // 子组件
export default {
    components: {
        SubModel
    },
    beforeDestroy() {
        console.log('父组件', 'beforeDestroy');
    },
    destroyed() {
        console.log('父组件', 'destroyed');
    }
};
</script>

<style lang="less" scoped></style>

sub-model.vue

<template>
    <!-- 子组件 -->
    <div class="views__home__sub-model">

    </div>
</template>

<script>
export default {
    beforeDestroy() {
        console.log('子组件', 'beforeDestroy');
    },
    destroyed() {
        console.log('子组件', 'destroyed');
    }
};
</script>

<style lang="less" scoped></style>

打印结果

e52ce20180c7180633f1dd00308958d1.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值