美文网首页
对vue生命周期的理解

对vue生命周期的理解

作者: Fastrider | 来源:发表于2019-05-14 21:23 被阅读0次

官网给出了如下图示:


image1.png
image2.png

由该图我们了解到vue的生命周期有8种,分别是 beforeCreate, created, beforeMount,mounted, beforeUpdate, updated,destroyed。所以我们创建一个简单的vue实例,在不同的生命周期下打印this.message,this.$el,观察dom渲染和挂载情况。

代码示例:

<!DOCTYPE html>
<html>
<head>
  <title>2333</title>
  <script type="text/javascript" src='../vue.js'></script>
  <script type="text/javascript">
    window.onload = function(){
     var app = new Vue({
      el: '#app',
      data: {
          message : "xuxiao is boy" 
      },
       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('真实dom结构:' + document.getElementById('app').innerHTML);
            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('真实dom结构:' + document.getElementById('app').innerHTML);
            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>
</head>
<body>
  <div id="app">
    <input type="text" name="sss" v-model="message"/>
  {{message}}
  </div>
</body>
</html>

截图如下:


图1.1 html样式 图1.2 打印结果

当改变输入框的内容时:


图2.1 update变化前后

注意:关于this.message在update前后值相同的情况,我认为因为这个并不是监听前后的变化,而是你在修改前这个值,因为只要修改就会触发更新前和后,这个值已经改变了,所以拿到的都是改后的值。所以要监测前后变化还是应该尝试打印dom。

参考博文链接:https://segmentfault.com/q/1010000011521681

相关文章

  • vue学习记录

    vue全家桶 vue生命周期 生命周期的理解 新建项目 ①:cnpm install vue-cli -g (全...

  • 浅谈vue的生命周期

    生命周期 在使用vue的过程中,对vue的生命周期的理解是最基础的开始,也许你不需要全部理解,但对其中最主要的几个...

  • Vue生命周期

    一.个人理解对Vue的生命周期的理解 1.1: beforeCreate: 创建阶段的第一个生命周期函数,组件的P...

  • vue面试必会

    一、对于MVVM的理解? 二、 Vue的生命周期 1.什么是Vue生命周期? 2.vue生命周期的作用是什么? 3...

  • Vue生命周期小结

    Vue框架已日臻成熟,生命周期也算是老生常谈了。网路上也有很多对Vue生命周期的讲解。此处是补充上自己的理解,再次...

  • Day45/100 Vue的生命周期

    写在前面的话 Vue生命周期贯穿Vue的整体思想的理解~灰常重要 (一)Vue生命周期全景图 (二)新建Vue实例...

  • vue的生命周期

    目录:1.对于MVVM的理解2.Vue的生命周期3.什么是vue生命周期?4.vue生命周期的作用是什么?5.vu...

  • Vue2.0生命周期钩子

    给大家分享一下我对Vue2.0的生命周期钩子的理解与用法 一、生命周期图示 二、生命周期分解 1、beforeCr...

  • 前端面试问题

    1.对于MVVM的理解 2.开发中常用的指令有哪些 3.请详细说下你对vue生命周期的理解 4.vue-route...

  • Vue生命周期

    我对vue生命周期的理解可以归纳为一句话,就是~~在正确的时间做正确的事 随着vue版本的更新,生命周期也在发生变...

网友评论

      本文标题:对vue生命周期的理解

      本文链接:https://www.haomeiwen.com/subject/erxyaqtx.html