vue3之生命周期钩子

Vue 3中,Options API (选项型)和 Composition API (组合型)生命周期钩子的图表:

1、使用 Options API,生命周期钩子作为选项暴露在Vue 实例上。 不需要引入任何东西, 例如:

<script>
  export default {
     mounted() {
        console.log('mounted!')
     },
     updated() {
        console.log('updated!')
     }
  }
</script>

2、使用 Composition API, 需要从vue中引入,例如:

<script>
  import { onMounted } from 'vue'

  export default {
     setup () {
       onMounted(() => {
         console.log('mounted in the composition api!')
       });

       onUpdated(() => {
         console.log('updated in the composition api!')
       });
     }
  }
</script>

 beforeCreate and created ,are replaced by the setup method itself.

setup中可以使用的9种生命周期钩子
onBeforeMountcalled before mounting begins
onMountedcalled when component is mounted
onBeforeUpdatecalled when reactive data changes and before re-render
onUpdatedcalled after re-render
onBeforeUnmountcalled before the Vue instance is destroyed
onUnmountedcalled after the instance is destroyed
onActivatedcalled when a kept-alive component is activated
onDeactivatedcalled when a kept-alive component is deactivated
onErrorCapturedcalled when an error is captured from a child component

3、

vue2和vue3中生命周期钩子的变更(选项型)
vue2vue3
beforeCreate setup
created setup
beforeMount beforeMount
mounted mounted
beforeUpdate beforeUpdate
updated updated
beforeDestroy beforeUnmount
destroyed unmounted
errorCaptured errorCaptured

更多详情请参考下列文章: 

 参考文章:A Complete Guide to Vue Lifecycle Hooks - with Vue 3 Updates - LearnVue

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值