Skip to content

Commit 4f456ee

Browse files
committed
chore(ci): blog sync
1 parent 20d4e6e commit 4f456ee

5 files changed

+1714
-8
lines changed

data/blog/BFChegaodutaxian.mdx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,29 @@
88
## 高度塌陷
99
我们知道,如果一个块级元素没有设置固定的高度,那么它的高度会被高度最大的子元素撑起,如下:
1010
在容器中有 box1 和 box2 两个元素,容器的高度被 box1 撑起
11-
```
11+
1212
<div style="background-color: aqua;display:flex">
1313
<div style="height:100px;width:100px;background-color: red;line-height:100px;text-align:center;color:white">box1</div>
1414
<div style="height:50px;width:150px;background-color: blue;line-height:50px;text-align:center;color:white">box2</div>
1515
</div>
16-
```
16+
1717
如果给 box1 添加浮动、定位等,使其脱离了正常的文档流,即产生了高度塌陷
18-
```
18+
1919
<div style="background-color: aqua;">
2020
<div style="height:100px;width:100px;background-color: red;line-height:100px;text-align:center;color:white;float: left;">box1</div>
2121
<div style="height:50px;width:150px;background-color: blue;line-height:50px;text-align:center;color:white">box2</div>
2222
</div><br>
23-
```
23+
2424
## 外边距塌陷
2525
外边距塌陷也称为外边距合并,是指两个在正常流中相邻(兄弟或父子关系)的块级元素的外边距,组合在一起变成单个外边距,不过只有上下外边距才会有塌陷,左右外边距不会出现这种问题。
2626
### 兄弟元素
27-
```
2827
<div style="margin-bottom: 5px; background-color: rgb(255, 147, 147)">
2928
一寸光阴一寸金
3029
</div>
3130
<div style="margin-top: 30px; background-color: rgb(159, 159, 255)">
3231
寸金难买寸光阴
3332
</div>
34-
```
33+
3534
```
3635
<div style="margin-bottom: 5px; background-color: rgb(255, 147, 147)">
3736
一寸光阴一寸金
@@ -42,13 +41,11 @@
4241
```
4342
可以看到在有相邻 `margin-bottom``margin-top` 时,他们只会保留边距大的一个,而不是将其累加
4443
### 父子元素
45-
```
4644
<div style="background-color: rgb(224,224,224);height:300px;display:flow-root">
4745
<div style="background-color: rgb(122,122,222);width:200px;height:200px;">
4846
<div style="background-color: rgb(122,22,122);width:100px;height:100px;margin-top: 20px;"></div>
4947
</div>
5048
</div>
51-
```
5249
这种现象我们应该经常会遇到,无论是给父元素还是子元素边距,他们都会有边距。
5350
以上情形都可以通过 BFC 解决,那么什么是 BFC ?
5451

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: Vue源码——模版编译(二)
3+
date: 2023-12-23T13:32:13Z
4+
summary:
5+
tags: []
6+
---
7+
8+
## 前言
9+
在 vue 中
10+
/.
11+
.流程图
12+
./
13+
14+
## 模板的挂载
15+
有经验的同学,肯定都对 `$mount` 方法很眼熟,此方法能够将 template/el 挂载到 DOM 元素上;在前文所介绍的 `Vue 初始化中` 同样也调用过这个方法:
16+
```
17+
if (vm.$options.el) {
18+
vm.$mount(vm.$options.el);
19+
}
20+
```
21+
那么它是如何做到的呢?
22+
<a href="http://localhost:3000/detail/1080" target="_black">Vue 的挂载方法 $mount</a>
23+
24+
## 模板的编译
25+
### 模板编译函数
26+
`$mount`中获取到`template`后,我们调用了<a href="http://localhost:3000/detail/1081" target="_black">compileToFunctions</a>函数,将其处理成了`rander表达式`和 (阿巴巴巴)。
27+
这个函数的核心其实还是我们在外部传入的`basecompile`:
28+
```
29+
createCompilerCreator(function baseCompile(
30+
template: string,
31+
options: CompilerOptions
32+
): CompiledResult {
33+
const ast = parse(template.trim(), options);
34+
if (options.optimize !== false) {
35+
optimize(ast, options);
36+
}
37+
const code = generate(ast, options);
38+
return {
39+
ast,
40+
render: code.render,
41+
staticRenderFns: code.staticRenderFns,
42+
};
43+
});
44+
```
45+
在其中通过了多个高阶函数进行处理包装才得到了<a href="http://localhost:3000/detail/1081" target="_black">compileToFunctions</a>。
46+
47+
### baseCompile函数
48+
`baseCompile`中有着简短的核心代码,首先调用了<a href="http://localhost:3000/detail/1082" target="_black">parse</a>函数,在其中利用了正则对模板以及其中的 style、class、directives进行了解析,形成ast树
49+
50+

0 commit comments

Comments
 (0)