Vue 生命周期

2021-10-12  本文已影响0人  洪锦一
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期-生命周期,就是 Vue 实例从创建到销毁经过的一系列过程。</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
</head>

<body>
    <div id="app">
        <p>{{message}}</p>
        <button @click='edit'>修改</button>
        <button @click='destroy'>销毁</button>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                message: 'boy'
            },
            methods: {
                edit() {
                    this.message = 123;
                },
                destroy() { 
                    app.$destroy()
                }
            }, 
            beforeCreate() {
                console.group('beforeCreate 创建前状态===============>');
                console.log("%c%s", "color:blue", "el     : " + this.$el); //undefined
                console.log("%c%s", "color:red", "data   : " + this.$data); //undefined 
                console.log("%c%s", "color:red", "message: " + this.message)
            },
            created() {
                console.group('created 创建完毕状态===============>');
                console.log("%c%s", "color:red", "el     : " + this.$el); //undefined
                console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化 
                console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
            },
            beforeMount() {
                console.group('beforeMount 挂载前状态===============>');
                console.log("%c%s", "color:red", "el     : " + (this.$el)); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化  
                console.log("%c%s", "color:red", "message: " + this.message); //已被初始化  
            },
            mounted() {
                console.group('mounted 挂载结束状态===============>');
                console.log("%c%s", "color:red", "el     : " + this.$el); //已被初始化
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data); //已被初始化
                console.log("%c%s", "color:red", "message: " + this.message); //已被初始化 
            },
            beforeUpdate() {
                console.group('beforeUpdate 更新前状态===============>');
                console.log("%c%s", "color:red", "el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data);
                console.log("%c%s", "color:red", "message: " + this.message);
            },
            updated() {
                console.group('updated 更新完成状态===============>');
                console.log("%c%s", "color:red", "el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data);
                console.log("%c%s", "color:red", "message: " + this.message);
            },
            beforeDestroy() {
                console.group('beforeDestroy 销毁前状态===============>');
                console.log("%c%s", "color:red", "el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data);
                console.log("%c%s", "color:red", "message: " + this.message);
            },
            destroyed() {
                console.group('destroyed 销毁完成状态===============>');
                console.log("%c%s", "color:red", "el     : " + this.$el);
                console.log(this.$el);
                console.log("%c%s", "color:red", "data   : " + this.$data);
                console.log("%c%s", "color:red", "message: " + this.message)
            }

        })
    </script>
</body>

</html>
上一篇下一篇

猜你喜欢

热点阅读