• 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
  • 相关阅读:
    AI驱动的未来:探索人工智能的无限潜力 | 开源专题 No.39
    第十站:Java白——测试与调试的艺术
    webpack核心模块
    Typora Mac版本安装Pandoc导出文件为word格式(windows可通用)
    飞利浦Fidelio B97全景声家庭影院,让你在家享受“暑期档”
    Spring Boot (Vue3+ElementPlus+Axios+MyBatisPlus +Spring Boot 前后端分离)
    notion, followus, wolai, wiz, lark
    Linux TCP和UDP协议
    Chapter13 : Ultrahigh Throughput Protein-Ligand Docking with Deep Learning
    软件测试---产品需求文档测试
  • 原文地址:https://blog.csdn.net/weixin_46862327/article/details/125987560