链接地址:https://segmentfault.com/a/1190000011381906

首先,每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期。首先看一张图吧~这是官方文档上的图片相信大家一定都会很熟悉:

可以看到在vue一整个的生命周期中会有很多钩子函数提供给我们在vue生命周期不同的时刻进行操作, 那么先列出所有的钩子函数,然后我们再一一详解:

  • beforeCreate
  • created
  • beforeMount
  • mounted
  • beforeUpdate
  • updated
  • beforeDestroy
  • destroyed

先来一波代码,各位复制在浏览器中运行,打开console查看就行了:

<!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>

  

运行后打开console可以看到打印出来内容如

可以看到一个vue实例在创建过程中调用的几个生命周期钩子。

最新文章

  1. .NET面试题集锦②(Part 二)
  2. 读《我是IT小小鸟》后有感
  3. MvvmLight ToolKit .Net4.5版本 CanExecute不能刷新界面bug
  4. LeetCode Range Sum Query 2D - Mutable
  5. throw跟throws关键字
  6. websphere变成英文了
  7. spring.net异常处理
  8. Java web.xml加载顺序
  9. logstash 处理tomcat catalina.out
  10. Top 20 JavaScript Projects of 2017
  11. weblogic 密码加密
  12. Leetcode_204_Count Primes
  13. 剑指offer(javascript实现)
  14. Ceph集群搭建及Kubernetes上实现动态存储(StorageClass)
  15. /usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFReadRGBAStrip@LIBTIFF_4.0’未定义的引用
  16. vue路由独享守卫beforeEnter
  17. python(一)——初识与变量
  18. django-ajax post与get请求
  19. c#操作数据库的增删改查语句及DataGridView简单使用
  20. Eclipse实用小插件

热门文章

  1. Qt项目中main主函数及其作用
  2. Sql中使用With创建多张临时表
  3. C++ STL(标准模板库)
  4. 十、S3C2440 开发资源
  5. 使用QT创建系统托盘
  6. Nacos 快速开始
  7. 理解Python中的__init__和__new__
  8. SQL插入字段
  9. KCF跟踪算法
  10. TweenMax的GSAP(GreenSock动画平台)GSAP,专业的Web动画库