生命周期函数就是vue实例在某一个时间点会自动执行的函数
当我们创建一个实例的时候,也就是我们调用 new Vue() 这句话的时候,vue会帮助我们去创建一个实例,创建过程其实并不像我们想的那么简单,他要经过很多的步骤
Init(Events & Lifecycle):首先他会去初始化事件和生命周期相关的内容,当最基础的初始化完成的时候,在这个时间点上,vue会自动的帮我去之行一个函数,这个函数就是beforeCreate
beforeCreate:既然beforeCreate被自动之行,那么beforeCreate就是一个生命周期函数
var vm = new Vue({
  el:'#app',
  beforeCreate:function(){
    console.log('before create')
  }
})
我们发现这个在控制台被自动输出了,就是vue自动执行了beforeCreate这个函数,处理完这个函数,vue会继续调用一个写外部的注入,包括双向绑定的相关内容
Init(injections & reactivity): 外部的注射,各种绑定的初始化,这部分初始化完成的时候,基本上vue实例的初始化操作都完成了,在这个结点上,又会有一个自动的函数被执行,这个函数的名字叫created
created:这也是一个生命周期函数,因为他完全符合生命周期函数的定义
var vm = new Vue({
  el:'#app',
  beforeCreate:function(){
    console.log('before create')
  },
  created:function(){
    console.log('created')
  }
})

可以看到beforeCreate执行之后,created也被自动的执行了,继续看这张图

Has 'el' options:是否有el这个选项
Has 'template' optioins: 是否有template这个属性
  no->Compile el's outerHtml as template: 如果实例里面没有tempalte这个属性,会把外部el挂载点的html当作模板
  yes->Compile template into render functoin: 如果实例里面有tempalte,这个时候就会用template去渲染
但是有了模板之后并没有直接渲染到页面上,在渲染之前,又自动到去执行了一个函数,这个函数是beforeMount
beforeMount:这个函数也是一个生命周期函数,也就是模板即将挂载到页面到一瞬间,beforeMount会被执行
var vm = new Vue({
  el:'#app',
  template:'<h1>hello</h1>',
  beforeCreate:function(){
    console.log('before create')
  },
  created:function(){
    console.log('created')
  },
  beforeMount:function(){
    console.log('before mount')
  }
})
可以看到beforeMount被执行了,在beforeMount执行完成后
Create vm.$el and replace 'el' width it: 模板结合数据会被挂载到页面上,当dom挂载到页面之上,这个时候又有一个生命周期函数被执行了
mounted:在beforeMount dom并没有渲染到页面上,在mounted dom已经被渲染到页面上了,这个时候可以做个实验
<div id='app'>
  hello world  
</div> <script>
  var vm = new Vue({
    el:'#app',
    template:'<h1>hello</h1>',
    beforeCreate:function(){
      console.log('before create')
    },
    created:function(){
      console.log('created')
    },
    beforeMount:function(){
      console.log(this.$el);
      console.log('before mount')
    },
    mounted:function(){
      console.log(this.$el);
      console.log('mounted')
    }
  })
</script>
看到在beforeMount输出当dom是<div id='app'>hello world</div>,,在mounted输出的dom是<h1>hello</h1>这也印证了上面这张图的内容,在beforeMount的时候页面还没有被渲染,在mounted的时候页面已经被渲染完毕了
beforeDestroy,destroyed:
var vm = new Vue({
  el:'#app',  
  template:'<h1>hello</h1>',
  beforeCreate:function(){
    console.log('before create')
  },
  created:function(){
    console.log('created')
  },
  beforeMount:function(){
    console.log(this.$el);
    console.log('before mount')
  },
  mounted:function(){
    console.log(this.$el);
    console.log('mounted')
  },
  beforeDestroy:function(){
    console.log('beforeDestroy')
  },
  destroyed:function(){
    console.log('destroyed')
  }
})
刷新页面完毕,这个时候会发现beforeDestroy,destroyed并没有被触发,那什么时候被触发呢
when vm.$destroy() is called:当destroy()这个方法被调用的时候会调用beforeDestroy,当全部销毁的时候,destroyed会被执行,那怎么让他执行呢,在控制台执行vm.$destroy()的时候会调用这两个函数,还没被销毁之前会调用beforeDestroy,已经被销毁后会调用destroyed这个函数
这张图上还有两个生命周期函数,叫做beforeUpdate和updated,这两个生命周期函数在什么时候执行呢
beforeUpdate,updated:
var vm = new Vue({
  el:'#app',
  template:'<h1>hello</h1>',
  beforeCreate:function(){
    console.log('before create')
  },
  created:function(){
    console.log('created')
  },
  beforeMount:function(){
    console.log(this.$el);
    console.log('before mount')
  },
  mounted:function(){
    console.log(this.$el);
    console.log('mounted')
  },
  beforeDestroy:function(){
    console.log('beforeDestroy')
  },
  destroyed:function(){
    console.log('destroyed')
  },
  beforeUpdate:function(){
    console.log('before updated')
  },
  updated:function(){
    console.log('updated')
  }
})
刷新页面看,发现这两个钩子函数其实并没有被执行,那为什么没有被执行呢,看图解释说是,when data changes,当数据发生改变的时候才会被执行
beforeUpdate:数据发生改变,还没有被渲染之前,beforeUpdate会被执行
updated:当数据重新渲染之后,updated这个生命周期函数会被执行
教程里面只有8个生命周期函数,实际上有11个生命周期函数

vuejs生命周期函数的更多相关文章

  1. Activity系列讲解---三大基本状态与七大生命周期函数

    简介:四大组件之一,在应用中一个Activity可以用来表示一个界面,可以理解为用户可视化界面,一个android应用必须通过Activity来运行和启动. 1.三大基本状态与七大生命周期函数 2.代 ...

  2. Cocos2d-x场景生命周期函数介绍

    层(Layer)的生命周期函数有如下: init().初始化层调用. onEnter().进入层时候调用. onEnterTransitionDidFinish().进入层而且过渡动画结束时候调用. ...

  3. ionic中的生命周期函数

    //ionic中的生命周期函数 onPageLoaded(){ //page初始化时 console.log("page 1 : page loaded"); } //在这里可以做 ...

  4. UIViewCotroller 的生命周期函数

    Viewcontroller 的所有生命周期函数 重写时 一定要先写 父类 方法 就是(super  +生命周期函数) LoadView ViewDidLoad ViewDidUnload: 在iOS ...

  5. 微信小程序 生命周期函数详解

    微信小程序 生命周期函数 小程序中 判断当前首页是从其他页面返回,还是由入口打开 由于小程序的数据在我们退出小程序时并没有得到释放,因此再次点击开来数据依然没有变成初始化 解决方法:在小程序 data ...

  6. React——组件的生命周期函数

    每一个组件都有一些生命周期函数. 当组件实例被创建并且会插入到DOM中,下面这些函数会被调用 constructor componentWillMount render componentDidMou ...

  7. Vue之生命周期函数和钩子函数详解

    在学习vue几天后,感觉现在还停留在初级阶段,虽然知道怎么和后端做数据交互,但是对对vue的生命周期不甚了解.只知道简单的使用,而不知道为什么,这对后面的踩坑是相当不利的.因为我们有时候会在几个钩子函 ...

  8. vue实例的生命周期函数

    Vue的生命周期函数通常分为以下三类: ①实例创建时的生命周期函数:②实例执行时的生命周期的函数:③实例销毁时的生命周期的函数. 代码与注释详解: <!DOCTYPE html> < ...

  9. vue(4)—— vue的过滤器,监听属性,生命周期函数,获取DOM元素

    过滤器 vue允许自定义过滤器,我个人认为,过滤器有两种,一种是对数据的清洗过滤,一种是对数据切换的动画过滤 数据切换的动画过滤 这里还是利用前面的动态组件的例子: 这里由于没办法展示动画效果,代码如 ...

随机推荐

  1. Deep Visualization:可视化并理解CNN(转)

    转载地址:https://zhuanlan.zhihu.com/p/24833574 一.前言 CNN作为一个著名的深度学习领域的“黑盒”模型,已经在计算机视觉的诸多领域取得了极大的成功,但是,至今没 ...

  2. SQLServer连接查询之Cross Apply和Outer Apply的区别及用法

    https://blog.csdn.net/wikey_zhang/article/details/77480118 先简单了解下cross apply的语法以及会产生什么样的结果集吧! 示例表: S ...

  3. java——数据结构

    底层数据结构: 数组 ArrayList 链表 LinkedList 应用数据结构: 二分搜索树 BST 最大堆/最小堆 MaxHeap/MinHeap 线段树 SegmentTree 字典树 Tri ...

  4. java——斗地主小游戏之洗牌发牌

    遇到的问题: 1.int和Integer的区别? 1)Integer是int的包装类,int则是java的一种基本数据类型 . 2)Integer变量必须实例化后才能使用,而int变量不需要 . 3) ...

  5. 转 sql profile 绑定 litera and move profile to another db l for spa

    SQL TYPE 1:for bind value sql , first create a good plan with literal and with good  profile. then u ...

  6. vue--钩子函数1

    最近在学习自定义指令,这里做个整理 vue允许注册自定义指令,在2.0中,代码复用和抽象的主要形式是组件.然而有的情况下仍需要对普通DOM元素进行底层操作,这时就会用到自定义指令. 全局指令direc ...

  7. Murano Weekly Meeting 2016.06.28

    Meeting time: 2016.June.28 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

  8. CI模板中php脚本的使用

    今天偶然发现,在CI的模板中能够直接使用CI自带的函数,并且可以直接调用controller里面的属性.案例: 控制器: public function test(){ $this->a = ' ...

  9. java连接数据库驱动代码综合共享

    1.Oracle8/8i/9i数据库(thin模式)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();S ...

  10. Spring Boot2中Spring Security导致Eureka注册失败

    将Spring Boot升级到2.0,Spring Cloud升级到Finchley.M8时,Eureka注册就报错了 Eureka Server配置: server.port=9011 spring ...