vue-生命周期函数

vue中所有的钩子函数

  • beforeCreate (创建前)
  • created (创建后)
  • beforeMount (载入前)
  • mounted (载入后)
  • beforeUpdate (更新前)
  • updated (更新后)
  • beforeDestroy (销毁前)
  • destroyed (销毁后)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie-edge">
        <title>vue生命周期</title>
        <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <input v-model="message"></input>
            <h1>{{message}}</h1>
        </div>
    </body>
    <script>
        var vm=new Vue({/*创建vue对象*/
            el:"#app",/**挂载目标**/
            data:{
                message:'Hello World!'
            },
            computed:{/*实现某一属性的实时计算*/
                messgae1:function(){
                    return this.message.spilt(" ").reverse().join(" ");
                }
            },
            watched:{/*检测某一属性值的变化*/
    
            },
            methods:{/*组件内部的方法*/
    
            },
            // 实例初始化之后(刚刚创建了实例,还没初始化data,methods,computed...)
            beforeCreate:function(){
                console.group('------beforeCreated创建前状态------');
                // console.log(this); // 在事件钩子中,this代表当前对象实例
                console.log("%c%s","coloe:red","el     : "+ this.$el); //undefined
                console.log("%c%s","color:red","data    :" + this.$data); //undefined
                //此时vue中有值但未被初始化,{{message}}未被解析
                console.log("%c%s","color:red","message: " + this.message); //undefined
            },
            /**
                        * 1. 在beforeCreate和created钩子之间, 程序开始监控Data对象数据的变化及vue内部的初始化事件
                        *
                        **/
                        // 创建了实例, 初始化了data, methods,computed...,并且启动了数据监听
                        created:function(){
                                console.group('------created创建完毕------');
                                console.log("%c%s","coloe:red","el     : "+ this.$el); //undefined
                                console.log("%c%s","color:red","data    :" + this.$data); //已被初始化
                                // 此时{{message}}还未被解析,我们可以手动添加msg的值
                                console.log("%c%s","color:red","message: " + this.message); //已被初始化
                            },
                            /**
                            * 2. 在created和beforeMount之间, 判断是否有el选项, 若有则继续编译,无,则暂停生命周期;
                            * 然后程序会判断是否有template参数选项,若有, 则将其作为模板编译成render函数. 若无, 则将外部html作为模板编译(template优先级比外部html高)
                            **/
                            // 在数据挂载之前,可以对数据做最后一次修改
                            beforeMount:function(){
                                console.group('------beforeMount挂载前状态------');
                                console.log("%c%s","coloe:red","el     : "+ this.$el); //已被初始化
                                console.log("%c%s","color:red","data    :" + this.$data); //已被初始化
                                console.log("%c%s","color:red","message: " + this.message); //已被初始化
                            },
                            /**
                            * 3. 在beforeMount和mounted之间. 程序将上一步编辑好的html内容替换el属性指向的dom对象或者选择相对应的html标签里面的内容
                            * 
                            **/
                            //数据挂载(渲染)之后,即model中的数据显示到了view中
                            mounted:function(){
                                console.group('------mounted 挂载结束状态------');
                                console.log("%c%s","coloe:red","el     : "+ this.$el); //已被初始化
                                console.log(this.$el);
                                console.log("%c%s","color:red","data    :" + this.$data); //已被初始化
                                // 此时在页面中打印出'Hello World!'
                                console.log("%c%s","color:red","message: " + this.message); //已被初始化
                            },
                            // 以上四个阶段在一次渲染之后就不会再执行了
                            /**
                            * 4. mounted和beforeUpdate之间, 程序实时监控数据变化
                            *
                            */
                            beforeUpdate:function(){
                                console.group('beforeUpdate 更新前状态=============>>');
                                console.log("%c%s","coloe:red","el     : "+ this.$el); 
                                console.log(this.$el);
                                console.log("%c%s","color:red","data    :" + this.$data); 
                                // 未执行,因为值没变,一旦发生改变,beforeUpdate就会被反复执行
                                console.log("%c%s","color:red","message: " + this.message); 
                            },
                            /**
                            * 5. beforeUpdate和updated之间, 实时更新dom
                            *
                            **/
                            // 模型发生改变,并且数据渲染之后执行,每次改变都会激发
                            updated: function(){
                                console.group('updated 更新完成状态============>>');
                                console.log("%c%s","coloe: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); 
                            },
                            beforeDestory:function(){
                                console.group('beforeDestory 销毁前状态============>>');
                                console.log("%c%s","coloe: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); 
                            },
                            /**
                            * 6. 实例销毁 (一般我们等不到它销毁)
                            *
                            **/
                            destoryed:function(){
                                console.group('destoryed 销毁完成状态============>>');
                                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>
    </html>

    看完没有明白的请看下边链接:

    https://segmentfault.com/a/1190000008010666

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值