• Vue生命周期你真的理解了吗?(必会面试题)


    🧑‍💻Vue生命周期

    🔥1.1Vue生命周期都有哪些?

    🔥生命周期执行时机
    🔥beforeCreate在组件实例被创建之初、组件的属性⽣效之前被调用
    🔥created在组件实例已创建完毕。此时属性也已绑定,但真实DOM还未⽣成,$el 还不可⽤
    🔥beforeMount在组件挂载开始之前被调⽤。相关的 render 函数⾸次被调⽤
    🔥mounted在 el 被新建的 vm.$el 替换并挂载到实例上之后被调用
    🔥beforeUpdate在组件数据更新之前调⽤。发⽣在虚拟 DOM 打补丁之前
    🔥 update在组件数据更新之后被调用
    🔥 activited在组件被激活时调⽤(使用了 的情况下)
    🔥 deactivated在组件被销毁时调⽤(使用了 的情况下)
    🔥 beforeDestory在组件销毁前调⽤
    🔥 destoryed在组件销毁后调⽤

    代码详情

    class Vue{
    	constructor( options ){
    		options.beforeCreate.call(this)
    		this.$data = options.data;
    		
    		options.created.call(this)
    		options.beforeMount.call(this)
    
    		this.$el = document.querySelector(options.el);
    		options.mounted.call(this)
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    new Vue({
      el:"#app",
      data : {
      	str:'123'
      },
      beforeCreate(){
      	console.log('beforeCreate',this.$el , this.$data)
      },
      created(){
      	console.log('created',this.$el , this.$data)
      },
      beforeMount(){
      	console.log('beforeMount',this.$el , this.$data)
      },
      mounted(){
      	console.log('mounted',this.$el , this.$data)
      },
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    🔥1.2 一旦进入组件或者一旦进入页面,会执行哪些生命周期?

    1. beforeCreate
    2. created在这里插入代码片
    3. beforeMount
    4. mounted

    🔥1.3 如果使用了keep-alive会多出来俩个生命周期

    1. activated
    2. deactivated

    🔥1.4 如果使用了keep-alive第一次进入组件会执行5个生命周期

    1. beforeCreate
    2. created
    3. beforeMount
    4. mounted
    5. activated

    🔥1.5 如果使用了keep-alive第二次或者第N次,每次都会执行一个生命周期

    activated

    🔥1.6 父子组件嵌套执行生命周期的顺序

    🔥挂载阶段

    1. beforeCreate
    2. created
    3. beforeMount
      1. beforeCreate
      2. created
      3. beforeMount
      4. mounted
    4. mounted

    🔥更新阶段

    1. beforeUpdate
      1. eforeUpdate
      2. updated
    2. updated

    🔥销毁阶段

    1. beforeDestroy
      1. beforeDestroy
      2. destroyed
    2. destroyed
  • 相关阅读:
    设计模式之原型模式
    [附源码]SSM计算机毕业设计学生实习管理系统JAVA
    渗透测试神器--Kali系统部署
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
    进销存软件如何解决服装行业问题与痛点
    c++ list容器使用详解
    告别os.path,拥抱pathlib
    仿大众点评——秒杀系统部分01
    使用Elasticsearch来进行简单的DDL搜索数据
    2.1、物理层的基本概念
  • 原文地址:https://blog.csdn.net/weixin_46862327/article/details/125987560