Vue的生命周期:
1.创建阶段
-
beforeCreate:初始化了部分参数,如果有相同的参数,做了参数合并,执行beforeCreate; -
created:初始化了Inject、Provide、props、methods、data、computed和watch,执行created; -
beforeMount:检查是否存在el属性,模板编译生成render函数,执行beforeMount;
判断用户是否传入了`el`选项,如果传入了则调用`$mount`函数进入模板编译与挂载阶段,如果没有传入`el`选项,则不进入下一个生命周期阶段,需要用户手动执行`vm.$mount`方法才进入下一个生命周期阶段。
mounted:生成Vnode,实例化Watcher,渲染dom,执行mounted; 2. 运行阶段beforeUpdate:在渲染dom后,执行了mounted钩子后,在数据更新的时候,执行beforeUpdate;updated:检查当前的watcher列表中,是否存在当前要更新数据的watcher,如果存在就执行updated; 3. 销毁阶段beforeDestroy:检查是否已经被卸载,如果已经被卸载,就直接return出去,否则执行beforeDestroy;destroyed:把所有有关自己痕迹的地方,都给删除掉;
图片制作不易 @这位大佬
父子组件的生命周期
father beforeCreate ->
father created ->
father beforeMounted->
son beforeCreate->
son created->
son beforeMounted->
son mounted->
father mounted
组件通信
- props/emit
- $children/$parent
- $ref
- eventbus 自定义事件
- vuex
- $attr
- slot
- provide/inject
- localStorege/sessionStorage
模板编译
- 会问到组件渲染更新过程(new watcher/diff/收集依赖)
- 涉及with语法
组件渲染更新过程
渲染:
将template生成ASt树(优化 AST)->AST(parse函数)生成render函数->touch/响应式getter/new Watch()收集依赖->render函数生成Vnode->patch|patch(elem,vnode)/diff|patch(newVnode,oldVnode)->渲染挂载
更新:
检测哪些依赖发生变化->触发依赖->重新执行render函数->patch
插槽
具名插槽
在子组件中的slot标签中写上name="xxx",会和父组件中的template模板中写上v-slot:xxx相对应,缩写#xxx
作用域插槽
在父组件中能够访问子组件中的内容,在子组件中绑定作用域,父组件的template中的v-slot:xxx='sonScope', 其中xxx搭配具名插槽使用