Skip to content

Commit 7ffb221

Browse files
committed
chore: adjust demo
1 parent 03facbd commit 7ffb221

File tree

2 files changed

+159
-7
lines changed

2 files changed

+159
-7
lines changed

demo/23-force-update.html

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<style>
2+
.demo {
3+
position: absolute;
4+
top: 50%;
5+
left: 50%;
6+
transform: translate(-50%, -50%);
7+
}
8+
.count {
9+
margin: 0 15px;
10+
}
11+
</style>
12+
13+
<body>
14+
<div id="app">
15+
<div class="demo">
16+
<button @click="minus">-1</button>
17+
<span class="count">{{ count }}</span>
18+
<button @click="plus">+1</button>
19+
</div>
20+
</div>
21+
</body>
22+
23+
<script src="./static/mini-vue.umd.js"></script>
24+
25+
<script>
26+
const { ref, effect, proxyRefs, compileToFunction } = MiniVue
27+
28+
29+
createApp({
30+
setup() {
31+
const count = ref(0)
32+
const plus = () => {
33+
count.value++
34+
}
35+
const minus = () => {
36+
count.value--
37+
}
38+
return {
39+
count,
40+
plus,
41+
minus
42+
}
43+
}
44+
}).mount('#app')
45+
46+
function createApp(options = {}) {
47+
const app = {
48+
mount(container) {
49+
if (typeof container === 'string') {
50+
container = document.querySelector(container)
51+
}
52+
const template = container.innerHTML
53+
const { render } = compileToFunction(template)
54+
const setupFn = options.setup || noop
55+
const setupResult = setupFn() || {}
56+
const data = proxyRefs(setupResult)
57+
const reload = () => {
58+
const vnode = render(data)
59+
container.innerHTML = ''
60+
_mount(vnode, container)
61+
}
62+
effect(() => {
63+
reload()
64+
})
65+
}
66+
}
67+
return app
68+
}
69+
70+
function _mount(vnode, container) {
71+
const el = document.createElement(vnode.tag)
72+
73+
if (vnode.props) {
74+
for (let key in vnode.props) {
75+
if (key.startsWith('on')) { // 事件绑定
76+
const eventName = key.slice(2).toLowerCase()
77+
el.addEventListener(eventName, vnode.props[key])
78+
} else {
79+
el.setAttribute(key, vnode.props[key])
80+
}
81+
}
82+
}
83+
if (Array.isArray(vnode.children)) {
84+
vnode.children.forEach(child => {
85+
_mount(child, el)
86+
})
87+
} else { // string
88+
el.textContent = vnode.children
89+
}
90+
91+
container.appendChild(el)
92+
}
93+
</script>
94+

slides.md

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,11 +2192,18 @@ function compileToFunction(template) {
21922192

21932193
[Vue3 Template Explorer](https://template-explorer.vuejs.org/)
21942194

2195+
---
2196+
layout: center
2197+
transition: fade-out
2198+
---
2199+
2200+
# 挂载&更新
2201+
21952202
---
21962203

2197-
## 挂载&更新
2204+
## 挂载
21982205

2199-
前面我们实现了 **响应式系统****编译**(丐中丐版),已经有能力将模板编译成渲染函数了,现在我们将它们整合起来,同时为了能将代码跑起来,我们还需要稍微简单实现下 **挂载和更新** (这里不涉及 patch)
2206+
前面我们实现了 **响应式系统****编译**(丐中丐版),已经有能力将模板编译成渲染函数了,现在我们将它们整合起来,同时为了能将代码跑起来,我们还需要稍微简单实现下 **挂载**
22002207

22012208
过程如下图所示:
22022209

@@ -2304,7 +2311,9 @@ mount(vnode, document.body)
23042311

23052312
---
23062313

2307-
前文中,我们已经实现了**挂载**,现在我们将代码封装一下,并实现自动**更新**的功能。
2314+
## 更新
2315+
2316+
前文中,我们已经实现了**挂载**,现在我们将代码封装一下,并实现**更新**的功能。
23082317

23092318
<div grid="~ cols-2 gap-2">
23102319

@@ -2334,9 +2343,7 @@ function createApp(options = {}) {
23342343
container.innerHTML = ''
23352344
_mount(vnode, container)
23362345
}
2337-
effect(() => {
2338-
reload()
2339-
})
2346+
effect(() => reload())
23402347
}
23412348
}
23422349
return app
@@ -2349,7 +2356,58 @@ function createApp(options = {}) {
23492356

23502357
现在代码应该可以跑起来了,并且能够响应式更新。
23512358

2352-
接下来来看一个 **计数器 demo**
2359+
但是这里的更新目前其实是: 先把 dom 清空,然后再重新挂载的一个过程。
2360+
2361+
来看一个 demo:
2362+
2363+
<div grid="~ cols-2 gap-2">
2364+
2365+
```html
2366+
<body>
2367+
<div id="app">
2368+
<div class="demo">
2369+
<button @click="minus">-1</button>
2370+
<span class="count">{{ count }}</span>
2371+
<button @click="plus">+1</button>
2372+
</div>
2373+
</div>
2374+
</body>
2375+
2376+
<script src="./static/mini-vue.umd.js"></script>
2377+
2378+
<script>
2379+
const { ref, effect, proxyRefs, compileToFunction } = MiniVue
2380+
</script>
2381+
```
2382+
2383+
```js
2384+
// demo: 23-force-update.html
2385+
2386+
createApp({
2387+
setup() {
2388+
const count = ref(0)
2389+
const plus = () => {
2390+
count.value++
2391+
}
2392+
const minus = () => {
2393+
count.value--
2394+
}
2395+
return {
2396+
count,
2397+
plus,
2398+
minus
2399+
}
2400+
}
2401+
}).mount('#app')
2402+
```
2403+
2404+
</div>
2405+
2406+
---
2407+
2408+
运行 demo,点击 `+1` 按钮,查看 DevTools 的 Elements,发现每次都是全量更新。
2409+
2410+
接下来我们来简单优化下,实现一个简单的 `patch` 函数,用来对比新旧节点,只更新需要更新的部分。
23532411

23542412
---
23552413

0 commit comments

Comments
 (0)