首先,每一个Vue实例在被建立以前都要通过一系列的初始化过程,这个过程就是vue的生命周期。首先看一张图吧~这是官方文档上的图片相信你们必定都会很熟悉:css
能够看到在vue一整个的生命周期中会有不少钩子函数提供给咱们在vue生命周期不一样的时刻进行操做, 那么先列出全部的钩子函数,而后咱们再一一详解:html
先来一波代码,各位复制在浏览器中运行,打开console查看就好了:vue
<!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实例在建立过程当中调用的几个生命周期钩子。app
在这个生命周期之间,进行初始化事件,进行数据的观测,能够看到在created的时候数据已经和data属性进行绑定(放在data中的属性当值发生改变的同时,视图也会改变)。
注意看下:此时仍是没有el选项dom
在这一阶段发生的事情仍是比较多的。函数
首先会判断对象是否有el选项。若是有的话就继续向下编译,若是没有el选项,则中止编译,也就意味着中止了生命周期,直到在该vue实例上调用vm.$mount(el)。此时注释掉代码中:学习
el: '#app',
而后运行能够看到到created的时候就中止了:ui
若是咱们在后面继续调用vm.$mount(el),能够发现代码继续向下执行了this
vm.$mount(el) //这个el参数就是挂在的dom接点
而后,咱们往下看,template参数选项的有无对生命周期的影响。
(1).若是vue实例对象中有template参数选项,则将其做为模板编译成render函数。
(2).若是没有template选项,则将外部HTML做为模板编译。
(3).能够看到template中的模板优先级要高于outer HTML的优先级。
修改代码以下, 在HTML结构中增长了一串html,在vue对象中增长了template选项:
<!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的生命周期' } </script> </html>
执行后的结果能够看到在页面中显示的是:
那么将vue对象中template的选项注释掉后打印以下信息:
这下就能够想一想什么el的判断要在template以前了~是由于vue须要经过el找到对应的outer template。
在vue对象中还有一个render函数,它是以createElement做为参数,而后作渲染操做,并且咱们能够直接嵌入JSX.
new Vue({ el: '#app', render: function(createElement) { return createElement('h1', 'this is createElement') } })
能够看到页面中渲染的是:
因此综合排名优先级:
render函数选项 > template选项 > outer HTML.
能够看到此时是给vue实例对象添加$el成员,而且替换掉挂在的DOM元素。由于在以前console中打印的结果能够看到beforeMount以前el上仍是undefined。
注意看下面截图:
在mounted以前h1中仍是经过{{message}}进行占位的,由于此时还有挂在到页面上,仍是JavaScript中的虚拟DOM形式存在的。在mounted以后能够看到h1中的内容发生了变化。
当vue发现data中的数据发生了改变,会触发对应组件的从新渲染,前后调用beforeUpdate和updated钩子函数。咱们在console中输入:
vm.message = '触发组件更新'
发现触发了组件的更新:
beforeDestroy钩子函数在实例销毁以前调用。在这一步,实例仍然彻底可用。
destroyed钩子函数在Vue 实例销毁后调用。调用后,Vue 实例指示的全部东西都会解绑定,全部的事件监听器会被移除,全部的子实例也会被销毁。
本文是我的对vue的生命周期的理解,有什么不对的地方还请大牛多多指点~