当前位置:首页 > 编程笔记 > 正文
已解决

Vue3生命周期详解,Vue2和Vue3对比

来自网友在路上 173873提问 提问时间:2023-10-12 09:56:54阅读次数: 73

最佳答案 问答题库738位专家为你答疑解惑

一、Vue3中的生命周期

  1. setup() : 开始创建组件之前,在 beforeCreate 和 created 之前执行,创建的是 data 和 method
  2. onBeforeMount() : 组件挂载到节点上之前执行的函数
  3. onMounted() : 组件挂载完成后执行的函数
  4. onBeforeUpdate(): 组件更新之前执行的函数
  5. onUpdated(): 组件更新完成之后执行的函数
  6. onBeforeUnmount(): 组件卸载之前执行的函数
  7. onUnmounted(): 组件卸载完成后执行的函数
  8. onActivated(): 被包含在 中的组件,会多出两个生命周期钩子函数,被激活时执行
  9. onDeactivated(): 比如从 A 组件,切换到 B 组件,A 组件消失时执行
  10. onErrorCaptured(): 当捕获一个来自子孙组件的异常时激活钩子函数

二、Vue2.X和Vue3.X对比

vue2           ------->      vue3beforeCreate   -------->      setup(()=>{})
created        -------->      setup(()=>{})
beforeMount    -------->      onBeforeMount(()=>{})
mounted        -------->      onMounted(()=>{})
beforeUpdate   -------->      onBeforeUpdate(()=>{})
updated        -------->      onUpdated(()=>{})
beforeDestroy  -------->      onBeforeUnmount(()=>{})
destroyed      -------->      onUnmounted(()=>{})
activated      -------->      onActivated(()=>{})
deactivated    -------->      onDeactivated(()=>{})
errorCaptured  -------->      one rrorCaptured(()=>{})

总结: Vue2和Vue3钩子变化不大,beforeCreate 、created 两个钩子被setup()钩子来替代。

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Vue3生命周期详解,Vue2和Vue3对比":http://eshow365.cn/6-19152-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!