zoukankan      html  css  js  c++  java
  • vue

    Vue 实例有一个完整的生命周期,生命周期也就是指一个实例从开始创建到销毁的这个过程。

    • beforeCreated():在实例创建之间执行,数据未加载状态。

    • created():在实例创建、数据加载后,能初始化数据,DOM 渲染之前执行。

    • beforeMount():虚拟 DOM 已创建完成,在数据渲染前最后一次更改数据。

    • mounted():页面、数据渲染完成,真实 DOM 挂载完成。

    • beforeUpadate():重新渲染之前触发。

    • updated():数据已经更改完成,DOM 也重新 render 完成,更改数据会陷入死循环。

    • beforeDestory() 和 destoryed():前者是销毁前执行(实例仍然完全可用),后者则是销毁后执行。

    如下图:

    代码实践

     // 生命周期可查看控制台打印 - 详细信息
       beforeCreate: function() {
          console.group('------beforeCreate创建前状态------');
          console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
          console.log("%c%s", "color:red","data   : " + this.$data); //undefined 
          console.log("%c%s", "color:red","message: " + this.message) 
        },
        created: function() {
          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: function() {
          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: function() {
          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: function () {
          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: function () {
          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: function () {
          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: function () {
          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)
        }
  • 相关阅读:
    git创建一个空的版本库
    程序后台服务启动,MongoDB未启动(启动较慢)/(关机重启情况下)。
    启动客户端后台服务
    客户端后台服务(已注册机器)RabbitMQ未消费的情况
    MongoDB数据重复解决方案
    github中新建一个branch(分支)
    MES-后台服务卸载
    linux 第八章 高级键盘
    socketserver
    jmeter发送邮件的模板
  • 原文地址:https://www.cnblogs.com/hai-cheng/p/7825938.html
Copyright © 2011-2022 走看看