生命周期函数
我们都知道,在Vue2.x中有8个生命周期函数:
○ beforeCreate
○ created
○ beforeMount
○ mounted
○ beforeUpdate
○ updated
○ beforeDestroy
○ destroyed
在vue3中,新增了一个setup生命周期函数,setup
执行的时机是在beforeCreate
生命函数之前执行,因此在这个函数中是不能通过this
来获取实例的;同时为了命名的统一,将beforeDestroy
改名为beforeUnmount
,destroyed
改名为unmounted
,因此vue3有以下生命周期函数:
○ beforeCreate(建议使用setup代替)
○ created(建议使用setup代替)
○ setup
○ beforeMount
○ mounted
○ beforeUpdate
○ updated
○ beforeUnmount
○ unmounted
同时,vue3新增了生命周期钩子,我们可以通过在生命周期函数前加on
来访问组件的生命周期,我们可以使用以下生命周期钩子:
○ onBeforeMount
○ onMounted
○ onBeforeUpdate
○ onUpdated
○ onBeforeUnmount
○ onUnmounted
○ onErrorCaptured
○ onRenderTracked
○ onRenderTriggered
那么这些钩子函数如何来进行调用呢?我们在setup中挂载生命周期钩子,当执行到对应的生命周期时,就调用对应的钩子函数:
说完生命周期,下期就是我们期待的Vue3新增加的那些功能。