zoukankan      html  css  js  c++  java
  • 关于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">
        <!--html中修改的-->
        <h1>{{message + '这是在outer HTML中的'}}</h1>
      </div>
    </body>
    <script>
      var vm = new Vue({
        el: '#app',
        template: "<h1>{{message +'这是在template中的'}}</h1>", //在vue配置项中修改的
        data: {
          message: 'Vue的生命周期'
        },
        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)
        }
      })
    </script>
    </html>

    调试结果如下:

    由上图清晰可见:

    beforeMount(页面载入前),展示的仅为定义的变量
    mounted(页面载入后),方可展示真正的数据

  • 相关阅读:
    诺基亚e71收不到彩信的解决办法
    calloc(), malloc(), realloc(), free(),alloca()
    win7下移动硬盘不能安全删除的解决方法
    C语言指针总结
    复制和剪切到底谁快?
    IC设计流程
    请用真正的原版电驴(eMule)!VeryCD是假电驴!
    [置顶] 图片检索,船,古代, 三桅, 三角帆船,侧滑浆
    [置顶] "在证书存储区中找不到清单签名证书"问题分析以及解决方案
    [置顶] sql2008 用户映射问题
  • 原文地址:https://www.cnblogs.com/yuwen1995/p/9755850.html
Copyright © 2011-2022 走看看