vue3生命周期

Vue3中,组件的生命周期有了新的变化。setup函数替代了beforeCreate和created,它的执行时间位于beforeMount和mounted之间。现在,生命周期函数需要从onBeforeMount等钩子中导入并直接调用。
摘要由CSDN通过智能技术生成

Vue2中提供了生命周期钩子函数,如beforeCreatecreatedbeforeMountmountedbeforeUpdateupdatedbeforeDestorydestoryed等,用于在组件不同阶段执行一些我们想要的执行的操作

到了Vue3,有所变化

  1. beforeCreate ---->setup
  2. created ---->setup
  3. beforeMount ---->onBeforeMount
  4. mounted ---->onMounted
  5. beforeUpdate ---->onBeforeUpdate
  6. updated ---->onUpdated
  7. beforeDestory ---->onBeforeUnmount
  8. destoryed ---->onUnmounted

可以看到,setup 函数代替了 beforeCreatecreated 两个生命周期函数,因此我们认为setup的执行时间在beforeCreatecreated 之间

Vue3的生命周期函数都是从 vue 中导入,再进行直接调用

//从 vue 中引入 多个生命周期函数
import {onBeforeMount, onMounted, onBeforeUpdate, onUpdated, 
  onBeforeUnmount, unMounted} from 'vue'
export default {
  name: 'App',
  setup() {
    onBeforeMount(() => {
      // 在挂载前执行
    })
    onMounted(() => {
      // 在挂载后执行
    })
    onBeforeUpdate(() => {
      // 在更新前前执行
    })
    onUpdated(() => {
      // 在更新后执行
    })
    onBeforeUnmount(() => {
      // 在组件销毁前执行
    })
    unMounted(() => {
      // 在组件销毁后执行
    })
    return {}
  }
  
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞瑞小同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值