知识点
- beforeCreate和setup如果同为配置项,setup先于beforeCreate执行。
- 组合式api,你需要先通过引入,然后在setup中使用。都是一个个的函数。
- 如果同一个生命周期钩子,在setup里面配置,同时也做了配置项,那么会发现在setup中执行的早于做配置项的。
- 父子生命周期执行顺序
- 挂载阶段
父beforeCreate -> 父created -> 父beforeMount -> 子beforeCreate -> 子created -> 子beforeMount -> 子mounted -> 父mounted - 更新阶段
父beforeUpdate -> 子beforeUpdate -> 子updated -> 父updated - 销毁阶段
父beforeDestroy -> 子beforeDestroy -> 子destroyed -> 父destroyed
- template配置项中可以写html结构,一般我们会写在<template>标签中,而且在脚手架中,如果写template配置项去渲染<App/>组件会有问题,因为引入的vue是运行时的vue缺少模板解析器,这时借助了render函数,如果引入完整版的vue没有这个问题。
-
vue2生命周期图
-
vue3生命周期图
- 关系
- Vue3.0中可以继续使用Vue2.x中的生命周期钩子,但有有两个被更名:
beforeDestroy改名为 beforeUnmount
destroyed改名为 unmounted
- Vue3.0也提供了 Composition API 形式的生命周期钩子,与Vue2.x中钩子对应关系如下:
beforeCreate===>setup()
created=======>setup()
beforeMount ===>onBeforeMount
mounted=======>onMounted
beforeUpdate===>onBeforeUpdate
updated===>onUpdated
beforeUnmount ==>onBeforeUnmount
unmounted =====>onUnmounted - 同时我们可以看到vue3取消了判断el的过程,在vue2中,即使没有el,我们也是可以走beforeCreate和created的,但是在vue3中就不行了。