3.vue生命周期钩子函数有哪些?(vue生命周期的理解)

定义:vue的生命周期是指vue实例从初始化创建到实例销毁的过程。期间会有8个钩子函数的调用。 vue的钩子函数图解: vue的钩子函数使用总结:1、beforeCreate(创建前):beforeCreate钩子函数,这个时候,vue实例的挂载元素$el和数据对象data都为undefined,还未初始化。无法访问到数据和真实的dom和data中的数据,可以在这里面使用l…

大家好,又见面了,我是你们的朋友全栈君。

定义:vue的生命周期是指vue实例从初始化创建到实例销毁的过程。期间会有8个钩子函数的调用。

 

vue的钩子函数图解:

3.vue生命周期钩子函数有哪些?(vue生命周期的理解)

 

vue的钩子函数使用总结:

1、beforeCreate(创建前):beforeCreate钩子函数,这个时候,vue实例的挂载元素$el和数据对象data都为undefined,还未初始化。无法访问到数据和真实的dom和data中的数据,可以在这里面使用loading

2、created(创建后):created函数中可以对data对象里面的数据进行使用和更改,不会触发其他的钩子函数,一般可以在这里做初始数据的获取也可以结束loading; 这里进行dom操作需要使用vue.nextTick()方法

3、beforeMount(挂载前):beforeMount钩子函数,vue实例的$el和data都初始化了,但还是虚拟的dom节点,具体的data.filter还未替换。在这里也可以更改数据,不会触发其他的钩子函数,一般可以在这里做初始数据的获取

4、mounted(挂载后):mounted钩子函数,此时,组件已经出现在页面中数据、真实dom都已经处理好了,事件都已经挂载好了,data.filter成功渲染可以在这里操作真实dom等事情…

5、beforeUpdate (更新前):当组件或实例的数据更改之后,会立即执行beforeUpdate,然后vue的虚拟dom机制会重新构建虚拟dom与上一次的虚拟dom树利用diff算法进行对比之后重新渲染,一般不做什么事儿

6、updated(更新后):当更新完成后,执行updated,数据已经更改完成,dom也重新render完成,可以操作更新后的虚拟dom

7、beforeDestroy(销毁前):当经过某种途径调用$destroy方法后,立即执行beforeDestroy,一般在这里做一些善后工作,例如清除计时器、清除非指令绑定的事件等等

8、destroyed(销毁后):vue实例解除了事件监听以及和dom的绑定(无响应了),但DOM节点依旧存在。这个时候,执行destroyed,在这里做善后工作也可以

 

vue的钩子函数代码验证:

console打印效果:

3.vue生命周期钩子函数有哪些?(vue生命周期的理解)

demo验证代码:

<template>
  <div>
    <h3>{
  
  {testMsg}}</h3>
    <p style="color:red;" id="testNum">{
  
  {testNum}}</p>
    <div><button @click="changNum()">点击修改num的数值大小</button></div>
    <p style="color:red;">过滤器处理后的值:{
  
  {testNum | add(10,20)}}</p>
    <div><button @click="destroyVue()">销毁</button></div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',

  // data是数据对象
  data () {   
    return {
      testMsg:"原始值",
      num:2,
    }
  },

  //computed对象包括需要计算的属性,属性值依赖于别的数据
  computed:{
    testNum:function(){
      let that =this;
      return that.num * 3;
    },
  },

  //检测某一属性值的变化,属性值的变化会造成其他dom变化
  watch:{
    testNum:function(val){
      if(val>9){
        console.log("testNum的值变得大于9了!");
      }
    }
  },

  //组件内部的方法
  methods:{
    changNum:function(){
      let that =this;
      that.num=5;
    },
    destroyVue:function(){
      this.$destroy();
    }
  },

  //过滤器对象,filter的第一个参数默认是当前的item值
  filters:{
    add:function(val,num1,num2){
      return val+num1+num2;
    },
  },

  //vue实例创建前
  beforeCreate:function(){
    console.group('beforeCreate 创建前状态===============》'); //console的分组打印
    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","testMsg: " + this.testMsg);//输出undefined
    console.log("%c%s", "color:red","testNum: " + this.testNum);//输出undefined
    console.groupEnd(); 
  },

  //vue实例创建完成,可以进行data对象中数据操作,一般获取初始化数据
  created:function(){
    console.group('created 创建完成状态===============》');
    console.log("%c%s", "color:red","el     : " + this.$el); //输出undefined
    console.log("%c%s", "color:red","data   : " + this.$data); //输出[Object Object] ,初始化成功
    console.log("%c%s", "color:red","testMsg: " + this.testMsg);//输出:"原始值",初始化成功
    console.log("%c%s", "color:red","testNum: " + this.testNum);//输出:6,可以computed计算
    console.groupEnd(); 
  },

  //vue实例挂载前,不能获取$el元素,生成的虚拟dom
  beforeMount:function(){
    console.group('beforeMount 挂载前状态===============》');
    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","testMsg: " + this.testMsg);//初始化成功
    console.log("%c%s", "color:red","testNum: " + this.testNum);//初始化成功
    console.log($("#testNum").text());  //无输出,dom未生成
    console.groupEnd(); 
  },

  //
  mounted:function(){
    console.group('mounted 挂载完成的状态===============》');
    console.log("%c%s", "color:red","el     : " + this.$el); //输出[object HTMLDivElement],初始化成功
    console.log("%c%s", "color:red","data   : " + this.$data); //初始化成功
    console.log("%c%s", "color:red","testMsg: " + this.testMsg);//初始化成功
    console.log("%c%s", "color:red","testNum: " + this.testNum);//初始化成功
    console.log($("#testNum").text());  //输出6,初始化成功
    console.groupEnd(); 
  },

  //更改data对象中数据后,页面渲染新数据前的状态
  beforeUpdate:function(){
    console.group('beforeUpdate 更新前的状态===============》');
    console.log("%c%s", "color:red","el     : " + this.$el); //初始化成功
    console.log("%c%s", "color:red","data   : " + this.$data); //初始化成功
    console.log("%c%s", "color:red","testMsg: " + this.testMsg);//初始化成功
    console.log("%c%s", "color:red","testNum: " + this.testNum);//初始化成功
    console.log($("#testNum").text());  //初始化成功
    console.groupEnd(); 
  },

  //数据更改后并重新渲染后,dom也重新更新了
  updated:function(){
    console.group('updated 更新完成的状态==========');
    console.log("%c%s", "color:red","el     : " + this.$el); //初始化成功
    console.log("%c%s", "color:red","data   : " + this.$data); //初始化成功
    console.log("%c%s", "color:red","testMsg: " + this.testMsg);//初始化成功
    console.log("%c%s", "color:red","testNum: " + this.testNum);//初始化成功
    console.log($("#testNum").text());  //初始化成功
    console.groupEnd(); 
  },

  //销毁前
  beforeDestroy:function(){
    console.group('beforeDestroy 销毁前的状态');
    console.log("%c%s", "color:red","el     : " + this.$el); 
    console.log("%c%s", "color:red","data   : " + this.$data); 
    console.log("%c%s", "color:red","testMsg: " + this.testMsg);
    console.log("%c%s", "color:red","testNum: " + this.testNum);
    console.log($("#testNum").text());  
    console.groupEnd(); 
  },

  //销毁后修改data数据无效,生成的dom依旧存在
  destroyed(){
    console.group('destroyed 销毁完成的状态===============》');
    console.log("%c%s", "color:red","el     : " + this.$el); 
    console.log("%c%s", "color:red","data   : " + this.$data); 
    console.log("%c%s", "color:red","testMsg: " + this.testMsg);
    console.log("%c%s", "color:red","testNum: " + this.testNum);
    console.log($("#testNum").text());  
    console.groupEnd(); 
  },

}
</script>

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/128650.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mybatis:Creating a new SqlSession Closing non transactional SqlSession[通俗易懂]

    mybatis:Creating a new SqlSession Closing non transactional SqlSession[通俗易懂]CreatinganewSqlSessionSqlSession[org.apache.ibatis.session.defaults.DefaultSqlSession@42607e80]wasnotregisteredforsynchronizationbecausesynchronizationisnotactiveJDBCConnection[com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@56ebc6bb]w.

    2022年6月16日
    55
  • 我国人口老龄化预测_中国人口老龄人口比例

    我国人口老龄化预测_中国人口老龄人口比例1、国内人口增速逐步下滑我国人口总量将近14亿,增速持续降低,城镇化率接近60%。建国以来,随着经济社会发展,我国人口总量从1949年的5.4亿人,增长至2018年13.9亿人。从增速来看,70年代人口总量的增速迅速下滑至1.3%-1.5%左右,90年代以来,人口总量增速持续缓慢下滑,近年保持在0.5%左右,但2018年增速进一步降低至0.4%。另一方面,中国城镇化率不断提升,到2018年已经达到59.58%。2018年我国人口总量为13.9亿人,同比增长0.4%,城镇化率59.58%数据来

    2025年9月14日
    24
  • 设置java编译级别

    设置java编译级别右键项目—->properties—–>JavaComplier—->javaCompilancelevel
    —->1.6

    2022年6月1日
    44
  • dijkstra算法详解—简单易懂[通俗易懂]

    dijkstra算法详解—简单易懂[通俗易懂]dijkstra算法详解(迪杰斯特拉算法)~~简单易懂,代码附有详细注释,含动态演示图片

    2025年8月13日
    4
  • JBPM工作流引擎原理「建议收藏」

    JBPM工作流引擎原理「建议收藏」1.JBPM工作流引擎是用来做什么的首先要说明的一点是工作流引擎指的并不只是JBPM,JBPM只是工作流引擎的一种。JBPM利用JPDL流程定义语言将现实生活中处理事务的业务流程进行抽象,形成一套业务流程规则,只要处理该项业务就必须按照这个流程规则进行。举一个很简单的例子,就拿看医生来讲,看医生的整个流程必须是先挂号,再看病,再抓药,只要你进行看医生这个业务就必须按照这套流程进行。…

    2025年10月12日
    4
  • STM32 SPI详解[通俗易懂]

    STM32 SPI详解[通俗易懂]本文的程序是主控室STM32F107各种宏定义和文件会在末尾说明1、SPI简介SPI,是英语SerialPeripheralinterface的缩写,顾名思义就是串行外围设备接口。是Motorola首先在其MC68HCXX系列处理器上定义的。SPI接口主要应用在EEPROM,FLASH,实时时钟,AD转换器,还有数字信号处理器和数字信号解码器之间。SPI,是一种高速的,全双工,

    2022年6月18日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号