Skip to content

Commit 2d34ca3

Browse files
committed
chore: adjust
1 parent a209e40 commit 2d34ca3

File tree

3 files changed

+28
-41
lines changed

3 files changed

+28
-41
lines changed
File renamed without changes.

demo/static/mini-vue.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

slides.md

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,9 +2183,7 @@ function compileToFunction(template) {
21832183
</ul>
21842184
```
21852185

2186-
经过编译后,其对应的渲染函数和VNode分别如下:
2187-
2188-
<div grid="~ cols-2 gap-2">
2186+
经过编译后,其对应的渲染函数如下:
21892187

21902188
```js
21912189
function render() {
@@ -2204,6 +2202,16 @@ function render() {
22042202
}
22052203
```
22062204

2205+
---
2206+
2207+
执行渲染函数:
2208+
2209+
```js
2210+
render()
2211+
```
2212+
2213+
会生成下面的 **虚拟DOM**:
2214+
22072215
```js
22082216
// VNode
22092217
{
@@ -2222,15 +2230,14 @@ function render() {
22222230
}
22232231
```
22242232

2225-
</div>
2226-
22272233
---
22282234

2229-
渲染函数的执行会生成虚拟 DOM,接下来我们尝试手动将虚拟 DOM 渲染到页面上
2235+
有了虚拟DOM,现在我们尝试手动将其渲染到页面中去
22302236

22312237
<div grid="~ cols-2 gap-2">
22322238

22332239
```js
2240+
// 挂载
22342241
function mount(vnode, container) {
22352242
const el = document.createElement(vnode.tag)
22362243
if (vnode.props) {
@@ -2247,7 +2254,7 @@ function mount(vnode, container) {
22472254
vnode.children.forEach(child => {
22482255
mount(child, el)
22492256
})
2250-
} else {
2257+
} else { // text node
22512258
el.textContent = vnode.children
22522259
}
22532260
container.appendChild(el)
@@ -2266,8 +2273,19 @@ mount(vnode, document.body)
22662273

22672274
---
22682275

2276+
前文中,我们已经实现了**挂载**,现在我们将代码封装一下,并实现自动**更新**的功能。
2277+
22692278
<div grid="~ cols-2 gap-2">
22702279

2280+
```js
2281+
// 挂载
2282+
let _mount = mount
2283+
2284+
function mount(vnode, container) {
2285+
// ...
2286+
}
2287+
```
2288+
22712289
```js
22722290
function createApp(options = {}) {
22732291
const app = {
@@ -2280,13 +2298,11 @@ function createApp(options = {}) {
22802298
const setupFn = options.setup || noop
22812299
const setupResult = setupFn() || {}
22822300
const data = proxyRefs(setupResult)
2283-
22842301
const reload = () => {
2285-
const vnode = render(data, { h, _toDisplayString: function toString(val) { return val && val.toString() } })
2302+
const vnode = render(data)
22862303
container.innerHTML = ''
22872304
_mount(vnode, container)
22882305
}
2289-
// 副作用函数
22902306
effect(() => {
22912307
reload()
22922308
})
@@ -2296,35 +2312,6 @@ function createApp(options = {}) {
22962312
}
22972313
```
22982314

2299-
```js
2300-
// 挂载
2301-
function _mount(vnode, container) {
2302-
const el = document.createElement(vnode.tag)
2303-
if (vnode.props) {
2304-
for (let key in vnode.props) {
2305-
if (key.startsWith('on')) { // 事件绑定
2306-
const eventName = key.slice(2).toLowerCase()
2307-
el.addEventListener(eventName, vnode.props[key])
2308-
} else {
2309-
el.setAttribute(key, vnode.props[key])
2310-
}
2311-
}
2312-
}
2313-
if (Array.isArray(vnode.children)) {
2314-
if (vnode.children.length === 1 && typeof vnode.children[0] != 'object') {
2315-
el.textContent = vnode.children[0]
2316-
} else {
2317-
vnode.children.forEach(child => {
2318-
_mount(child, el)
2319-
})
2320-
}
2321-
} else { // string
2322-
el.textContent = vnode.children
2323-
}
2324-
container.appendChild(el)
2325-
}
2326-
```
2327-
23282315
</div>
23292316

23302317
---
@@ -2335,7 +2322,7 @@ function _mount(vnode, container) {
23352322

23362323
---
23372324

2338-
## counter 计数器 demo
2325+
## Counter 计数器 demo
23392326

23402327
demo: `22-counter.html`
23412328

0 commit comments

Comments
 (0)