美文网首页
vue生命周期

vue生命周期

作者: sunflower_07 | 来源:发表于2019-02-28 00:04 被阅读0次

vue的生命周期就是从,创建到销毁,有8个,分别是:创建前(beforeCreate)、创建完成(create)、挂载前(beforeMount)、挂载后(mounted)、更新前(beforeUpdate)、更新后(update)、销毁前(beforeDestroy)、销毁后(destroyed)

lifecycle.png

先看一段代码

<!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">
  <h1>{{message}}</h1>
</div>
</body>
<script>
var vm = new Vue({
  el: '#app',
  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>

  1. 在创建前状态(beforeCreate) ,这个时候
    el :undefind;
    data:undefind;
    message:undefind; 都是undefind;

2.创建完成(Create)
el:undefind;
data:[object object];
message: vue的生命周期

3.挂载前(beforeMount)
el:<div id="app"></div>;
data:[object object];
message: 页面的message 已经改变为{{message}}进行占位

4.挂载后 (mounted)
el:<div id="app"></div>;
data:[object object];
message: 页面的message 已经变化成Vue的生命周期

  1. 更新前(beforeUpdate)
    如果程序计算数据有变化时调用
    6.更新(update)
    页面数据已更新成最新数据
    7.销毁前( beforeDestroy)
    释放vue对象之前调用,beforeDestroy 钩子,
    这个时候实例依然可用
    8.销毁完成(destroyed)
    destroyed 钩子成功运行之后,vue实例指令的所有东西,都会解绑,所有事件也都会被移除,所有的实例也都会被销毁

这目前是个人对vue的生命周期的理解,如果有更好的理解,请多多赐教,日后并加以修改,谢谢。

相关文章

  • 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/onqcuqtx.html