vue 父子组件的生命周期

2020-06-01  本文已影响0人  spencer_sun

1 综述

代码验证

lifeCycle.vue(parent)

<template>
<div id="lifeCycle">
    <div>
        parent
    </div>
    <div>
        <span>parent count</span>{{count}}
    </div>
    <div>
        <button @click="handleClick">click add parent </button>
    </div>
    <div class="child">
        <child></child>
    </div>
</div>
</template>
<script>
import child from "./components/child";

export default {
    name: "lifeCycle",
    data: function() {
        return {
            count: 2
        };
    },
    methods: {
        handleClick() {
            this.count++;
        }
    },
    components: {
        child
    },
    beforeCreate() {
        console.log("parent BeforeCreated");
    },
    created() {
        console.log("parent created");
    },
    beforeMount() {
        console.log("parent beforeMounted");
    },
    mounted() {
        console.log("parent mounted");
    },
    beforeUpdate() {
        console.log("parent beforeUpdate");
    },
    updated() {
        console.log("parent updated");
    },
    beforeDestroy() {
        console.log("parent beforeDestroy");
    },
    destroyed() {
        console.log("parent destroyed");
    }
};
</script>
<style lang="scss">
</style>

child.vue

<template>
<div>
    <p>child</p>
    <div>
        {{count}}
    </div>
    <div>
        <button @click="handleClick">click add</button>
    </div>
    <div class="grandChild">
        <grandChild></grandChild>
    </div>
</div>
    
</template>

<script>
import grandChild from "./grandChild";
export default {
    name: "child",
    data: function() {
        return {
            count: 1
        };
    },
    methods: {
        handleClick() {
            this.count++;
        }
    },
    components: {
        grandChild
    },
    beforeCreate() {
        console.log("child BeforeCreated");
    },
    created() {
        console.log("child created");
    },
    beforeMount() {
        console.log("child beforeMounted");
    },
    mounted() {
        console.log("child mounted");
    },
    beforeUpdate() {
        console.log("child beforeUpdate");
    },
    updated() {
        console.log("child updated");
    },
    beforeDestroy() {
        console.log("child beforeDestroy");
    },
    destroyed() {
        console.log("child destroyed");
    }
};
</script>

<style>
</style>

grandChild.vue

<template>
<div>
    <p>child</p>
    <div>
        {{count}}
    </div>
    <div>
        <button @click="handleClick"> grandChild click add</button>
    </div>
</div>
    
</template>

<script>
export default {
    name: "grandChild",
    data: function() {
        return {
            count: 1
        };
    },
    methods: {
        handleClick() {
            this.count++;
        }
    },
    beforeCreate() {
        console.log("grandChild BeforeCreated");
    },
    created() {
        console.log("grandChild created");
    },
    beforeMount() {
        console.log("grandChild beforeMounted");
    },
    mounted() {
        console.log("grandChild mounted");
    },
    beforeUpdate() {
        console.log("grandChild beforeUpdate");
    },
    updated() {
        console.log("grandChild updated");
    },
    beforeDestroy() {
        console.log("grandChild beforeDestroy");
    },
    destroyed() {
        console.log("grandChild destroyed");
    }
};
</script>

<style>
</style>


运行结果


1.png 2.png 3.png
上一篇下一篇

猜你喜欢

热点阅读