vue3.0生命周期详细解析

一、图示vue2.0到vue3.0改变

  • beforeCreate => setup()
  • created => setup()
  • beforeMount => onBeforeMount
  • mounted => onMounted
  • beforeUpdate => onBeforeUpdate
  • updated => onUpdated
  • beforeDestroy => onBeforeUnmount
  • destoryed => onUnmounted
  • errorCaptured => onErrorCaputed

二、解析图示的改变
1、去掉了vue2.0中的 beforeCreate 和 created 两个阶段,同样的新增了一个 setup

2、beforeMount 挂载之前 改名 onBeforeMount

3、mounted 挂载之后 改名 onMounted

4、beforeUpdate 数据更新之前 改名 onBeforeUpdate

5、updated 数据更新之后 改名 onUpdated

6、beforeDestroy 销毁前 改名 onBeforeUnmount

7、destoryed 销毁后 改名 onUnmounted

8、errorCaptured 报错 改名 onErrorCaptured

因为 setup 是围绕 beforeCreate 和 created 生命周期钩子运行的,所以不需要显式地定义它们。换句话说,在这些钩子中编写的任何代码都应该直接在 setup 函数中编写。

这些函数接受一个回调函数,当钩子被组件调用时将会被执行:


export default {
  setup() {
    // mounted
    onMounted(() => {
      console.log('Component is mounted!')
    })
  }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值