彻底理解vue的钩子函数,vue的生命周期理解,什么是vue的生命周期,钩子函数

原创

彻底理解vue的钩子函数,vue的生命周期理解,什么是vue的生命周期,钩子函数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/jiang7701037/article/details/83118665
            </div>
                                                <!--一个博主专栏付费入口-->
         
         <!--一个博主专栏付费入口结束-->
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-4a3473df85.css">
                                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-4a3473df85.css">
            <div class="htmledit_views" id="content_views">
                                        <p>官方图(官方的图大家总是理解不了):</p>

        使用vue框架,需要在合适的时机做合适的事情,了解了vue对象的生命周期和钩子函数,才能知道,哪些事情应该咋哪个函数里做。

一、vue的生命周期的理解

  1. 生命周期

用人举例说明:

生命周期就是一个人的一生,此处我需要说的没有人情一点(哈哈)。

从人的出生,到成长,到工作,到死亡,就是人的一生,也叫一个人的生命周期。

     2. 对象的生命周期

在程序开发中,是要使用对象的。那么,对象的生命周期就是:从对象的创建,到使用对象,到对象的消亡就是对象的生命周期。

用人和对象进行类比(此处没有人性):

程序中的对象

人出生

New对象

人工作(ps:要人的目的就是为了工作,如果一个人不工作,不为国家做贡献,那就不是合格的人,活着没有意义)

使用对象的方法和属性(ps:new的对象的目的就是为了用它,用对象主要就是使用对象的方法和属性)

人死亡(ps:人没有用了,那就“去死吧”)

对象使用完就该消亡了(过河拆桥,不用了,那就不要了。)

 

3. Vue的生命周期

Vue实例,vue组件实例就是vue对象,也是对象。所以,vue的生命周期和对象的生命周期是同样的道理

 

二、vue生命周期经历的阶段

 

生命周期是有不同的阶段的,就像人一样,有幼儿期,童年期,少年期,青年期,中年期,老年期。每个阶段应该做不同的事情,但是每个人做的事情又不尽相同。

Vue对象的生命周期也分不同的阶段,不同的阶段也可以做不同的事情,但是不同的vue(组件)对象在不同的阶段做的事情也不尽相同,所以,每个vue组件的代码不相同。

Vue生命周期经历哪些阶段:

  1. 总体来说:初始化、运行中、销毁
  2. 详细来说:开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、销毁等一系列过程

 

三、生命周期经历的阶段和钩子函数

 

  1. 实例化vue(组件)对象:new Vue()
  2. 初始化事件和生命周期 init events 和 init cycle
  3. beforeCreate函数:

在实例初始化之后,数据观测 (data observer) 和 event/watcher 事件配置之前被调用。

即此时vue(组件)对象被创建了,但是vue对象的属性还没有绑定,如data属性,computed属性还没有绑定,即没有值。

此时还没有数据和真实DOM。

即:属性还没有赋值,也没有动态创建template属性对应的HTML元素(二阶段的createUI函数还没有执行)

     4. 挂载数据(属性赋值)

包括 属性和computed的运算,

      5. Created函数:

vue对象的属性有值了,但是DOM还没有生成,$el属性还不存在。

此时有数据了,但是还没有真实的DOM

       即:data,computed都执行了。属性已经赋值,但没有动态创建template属性对应的HTML元素,所以,此时如果更改数据不会触发updated函数

       如果:数据的初始值就来自于后端,可以发送ajax,或者fetch请求获取数据,但是,此时不会触发updated函数

    6. 检查

       1)检查是否有el属性
检查vue配置,即new Vue{}里面的el项是否存在,有就继续检查template项。没有则等到手动绑定调用vm.$mount()

完成了全局变量$el的绑定。

        2)检查是否有template属性

检查配置中的template项,如果没有template进行填充被绑定区域,则被绑定区域的el对象的outerHTML(即整个#app DOM对象,包括<div id=”app” >和</div>标签)都作为被填充对象替换掉填充区域

即:如果vue对象中有 template属性,那么,template后面的HTML会替换$el对应的内容。如果有render属性,那么render就会替换template。

即:优先关系时: render  >  template > el

7  beforeMount函数:

模板编译(template)、数据挂载(把数据显示在模板里)之前执行的钩子函数

此时 this.$el有值,但是数据还没有挂载到页面上。即此时页面中的{{}}里的变量还没有被数据替换

8.模板编译:用vue对象的数据(属性)替换模板中的内容

9. Monuted函数:

模板编译完成,数据挂载完毕

即:此时已经把数据挂载到了页面上,所以,页面上能够看到正确的数据了。

一般来说,我们在此处发送异步请求(ajax,fetch,axios等),获取服务器上的数据,显示在DOM里。

10. beforeUpdate函数:

组件更新之前执行的函数,只有数据更新后,才能调用(触发)beforeUpdate,注意:此数据一定是在模板上出现的数据,否则,不会,也没有必要触发组件更新(因为数据不出现在模板里,就没有必要再次渲染)

数据更新了,但是,vue(组件)对象对应的dom中的内部(innerHTML)没有变,所以叫作组件更新前

11. updated函数:

组件更新之后执行的函数

vue(组件)对象对应的dom中的内部(innerHTML)改变了,所以,叫作组件更新之后

12.  activated函数:keep-alive组件激活时调用

13.  deactivated函数:keep-alive组件停用时调用

14.  beforeDestroy:vue(组件)对象销毁之前

15.  destroyed:vue组件销毁后

四、测试代码


 
 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>vue生命周期学习 </title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h1>{{message}} </h1>
  12. <h1>count:{{count}} </h1>
  13. </div>
  14. <input id="btn01" type="button" value="测试" />
  15. </body>
  16. <script type="text/javascript" src="js/vue.min.js" > </script>
  17. <script>
  18. var vm = new Vue({
  19. el: '#app',
  20. data: {
  21. message: 'Vue的生命周期',
  22. age: 2
  23. },
  24. computed:{
  25. count: function(){
  26. return this.age+ 1;
  27. }
  28. },
  29. // template:"<p>vue对象中的template的内容</p>",
  30. // render: function(createElement) {
  31. // return createElement('h1', 'this is createElement')
  32. // },
  33. beforeCreate: function() {
  34. console.group( '------beforeCreate创建前状态------');
  35. console.log( "%c%s", "color:red" , "el : " + this.$el); //undefined
  36. console.log( "%c%s", "color:red", "data : " + this.$data); //undefined
  37. console.log( "%c%s", "color:red", "count : " + this.count); //undefined
  38. console.log( "%c%s", "color:red", "message: " + this.message)
  39. },
  40. created: function() {
  41. console.group( '------created创建完毕状态------');
  42. console.log( "%c%s", "color:red", "el : " + this.$el); //undefined
  43. console.log( "%c%s", "color:red", "data : " + this.$data); //已被初始化
  44. console.log( "%c%s", "color:red", "count : " + this.count); //undefined
  45. console.log( "%c%s", "color:red", "message: " + this.message); //已被初始化
  46. },
  47. //完成了el的绑定
  48. beforeMount: function() {
  49. console.group( '------beforeMount挂载前状态------');
  50. console.log( "%c%s", "color:red", "el : " + ( this.$el)); //已被初始化
  51. console.log( this.$el.innerHTML);
  52. console.log( "%c%s", "color:red", "data : " + this.$data); //已被初始化
  53. console.log( "%c%s", "color:red", "message: " + this.message); //已被初始化
  54. },
  55. mounted: function() {
  56. console.group( '------mounted 挂载结束状态------');
  57. console.log( "%c%s", "color:red", "el : " + this.$el); //已被初始化
  58. console.log( this.$el);
  59. console.log( this.$el.innerHTML);
  60. console.log( "%c%s", "color:red", "data : " + this.$data); //已被初始化
  61. console.log( "%c%s", "color:red", "message: " + this.message); //已被初始化
  62. },
  63. beforeUpdate: function () {
  64. console.group( 'beforeUpdate 更新前状态===============》');
  65. console.log( "%c%s", "color:red", "el : " + this.$el);
  66. console.log( this.$el.innerHTML);
  67. console.log( "%c%s", "color:red", "data : " + this.$data.message);
  68. console.log( "%c%s", "color:red", "message: " + this.message);
  69. }
  70. ,
  71. updated: function () {
  72. console.group( 'updated 更新完成状态===============》');
  73. console.log( "%c%s", "color:red", "el : " + this.$el);
  74. console.log( this.$el.innerHTML);
  75. console.log( "%c%s", "color:red", "data : " + this.$data);
  76. console.log( "%c%s", "color:red", "message: " + this.message);
  77. },
  78. // beforeDestroy: function () {
  79. // console.group('beforeDestroy 销毁前状态===============》');
  80. // console.log("%c%s", "color:red","el : " + this.$el);
  81. // console.log(this.$el);
  82. // console.log("%c%s", "color:red","data : " + this.$data);
  83. // console.log("%c%s", "color:red","message: " + this.message);
  84. // },
  85. // destroyed: function () {
  86. // console.group('destroyed 销毁完成状态===============》');
  87. // console.log("%c%s", "color:red","el : " + this.$el);
  88. // console.log(this.$el);
  89. // console.log("%c%s", "color:red","data : " + this.$data);
  90. // console.log("%c%s", "color:red","message: " + this.message)
  91. // }
  92. });
  93. document.getElementById( "btn01").onclick = function(){
  94. vm.message= "改了";
  95. }

五、模拟vue的构造函数(部分代码)

myVue.js


 
 
  1. class MyVue{
  2. constructor(obj){
  3. //默认值
  4. let defaultObj={
  5. data: null,
  6. computed: null,
  7. watch: null,
  8. beforeCreate: function(){
  9. },
  10. created: function(){
  11. },
  12. beforeMount: function(){
  13. },
  14. mounted: function(){
  15. }
  16. }
  17. for( let key in defaultObj){
  18. obj[key]? this[key]=obj[key]: this[key]=defaultObj[key];
  19. }
  20. //对象创建完毕已经有this了。
  21. this.beforeCreate();
  22. //挂载数据:
  23. //1)、把传入的data属性的值赋给this
  24. if(obj.data){
  25. for( let key in this.data){
  26. this[key] = obj.data[key];
  27. }
  28. this.$data = obj.data; //设置全局变量
  29. }
  30. //2)、计算属性
  31. if(obj.computed){
  32. for( let key in obj.computed){
  33. this[key] = obj.computed[key].call( this);
  34. }
  35. }
  36. //created函数
  37. this.created();
  38. //检查是否有el属性
  39. if(obj.el){
  40. this.el = $(obj.el);
  41. this.$el = $(obj.el); //设置全局变量
  42. }
  43. //检查是否有template属性
  44. if( this.template){
  45. //this.template = obj.template;
  46. // 动态创建template 里所有的html元素
  47. }
  48. //beforeMonute
  49. this.beforeMount();
  50. //用vue对象的数据(属性)替换模板中的内容
  51. //1)、替换data中的数据
  52. let html = this.el.innerHTML;
  53. for( let key in this.data){
  54. //用属性值替换,属性名(页面上用双花括号包起来的)
  55. html=html.replace( new RegExp( "{{"+key+ "}}", "g"), this[key]);
  56. }
  57. //2)、替换computed中的数据
  58. for( let key in this.computed){
  59. //用属性值替换,属性名(页面上用双花括号包起来的)
  60. html=html.replace( new RegExp( "{{"+key+ "}}", "g"), this[key]);
  61. }
  62. this.el.innerHTML = html;
  63. //mounted函数:
  64. this.mounted();
  65. }
  66. addWatch(){
  67. }
  68. //数据双向绑定
  69. //
  70. }
  71. function $(str){ //#box .cls p
  72. if(str.charAt( 0)== "#"){
  73. return document.getElementById(str.substring( 1));
  74. } else if(str.charAt( 0)== "."){
  75. return document.getElementsByClassName(str.substring( 1));
  76. } else{
  77. return document.getElementsByTagName(str);
  78. }
  79. }

html代码:

<body>

  <div id="app">

    <h1>{{message}}</h1>

    <h1>count:{{count}}</h1>

  </div>

</body>

<script type="text/javascript" src="js/myvue.js" ></script>

<script>

  var vm = new MyVue({

    el: '#app',

    data: {

      message: 'Vue的生命周期',

      age:1

    },

    computed:{

        count:function(){

           return this.age+1;

        }

    },

    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); //已被初始化

    },

    //完成了el的绑定

    beforeMount: function() {

       console.group('------beforeMount挂载前状态------');

       console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化

       console.log(this.$el);

       console.log(this.$el.innerHTML);   

       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(this.$el.innerHTML);   

      console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化

      console.log("%c%s", "color:red","message: " + this.message); //已被初始化

    }   

  })

文章最后发布于: 2018-10-17 22:32:02
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值