美文网首页极客
Vue生命周期

Vue生命周期

作者: 极客传 | 来源:发表于2019-02-26 22:17 被阅读1次

Vue 官网的示意图:

vue生命周期

通过代码来测试 Vue 生命周期的执行顺序:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://unpkg.com/vue"></script>
    
</head>
<body>

<div id="app">
     <p>{{ message }}</p>
</div>

<script type="text/javascript">
    
  var app = new Vue({
      el: '#app',
      data: {
          message : "this is vue lifecycle test" 
      },
       beforeCreate: function () {
              console.group('beforeCreate 创建前状态===============》');
              console.log("%c%s", "color:red", "this   : " + this); //[object Object]
              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)  //undefined 
        },
        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>
</body>
</html>

当声明一个 Vue 实例时,会先执行这个实例中的这几个方法:beforeCreate ——> created ——> beforeMount ——> mounted ;

  • beforeCreate,dom 元素 el 和 data 都未初始化;
  • created,初始化 data;
  • beforeMount,初始化 dom 元素 el

当 data 中的 message 发生改变时,会执行 beforeUpdate 和 updated;并且在 beforeUpdate 状态, data 和 el 的内容就已经改变;

页面created,组件created,组件mounted,页面mounted。

·


【1】https://segmentfault.com/a/1190000011381906?utm_source=tag-newest

相关文章

  • vue生命周期

    一 vue生命周期 Vue的生命周期:就是vue实例从创建到销毁的全过程 二 vue生命周期钩子 vue生命周期...

  • vue生命周期

    vue生命周期详: vue生命周期详解图: vue生命周期详解代码展示:

  • Vue生命周期

    Vue生命周期详解 一、Vue生命周期 与 作用 (生命周期钩子就是生命周期函数) (1)Vue生命周期:每个Vu...

  • vue3较vue2的特别之处 - 生命周期

    vue2 生命周期 vue3 生命周期 将 Vue2 的生命周期钩子代码更新到 Vue3 参考地址:https:/...

  • 前端之路-VUE面试题

    vue生命周期面试题vue 生命周期是什么? Vue 实例从创建到销毁的过程,就是生命周期 beforeCreat...

  • Vue.js入门(二):生命周期

    1 生命周期 Vue生命周期是Vue实例从创建到销毁的过程,就是生命周期。在Vue实例的生命周期过程中会运行生命周...

  • vue生命周期

    vue里的生命周期是什么? vue实例从创建到销毁的过程称之为vue的生命周期 vue的生命周期各阶段都做了什么?...

  • Vue 生命周期

    vue里的生命周期是什么? vue实例从创建到销毁的过程称之为vue的生命周期 vue的生命周期各阶段都做了什么?...

  • vue学习记录

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

  • Vue生命周期

    Vue生命周期 1、什么是vue生命周期? 答: Vue 实例从创建到销毁的过程,就是生命周期。也就是从开始创建、...

网友评论

    本文标题:Vue生命周期

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