Skip to content

Commit

Permalink
chore: adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
w2xi committed Jan 25, 2024
1 parent a209e40 commit 2d34ca3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 41 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion demo/static/mini-vue.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 27 additions & 40 deletions slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -2183,9 +2183,7 @@ function compileToFunction(template) {
</ul>
```

经过编译后,其对应的渲染函数和VNode分别如下:

<div grid="~ cols-2 gap-2">
经过编译后,其对应的渲染函数如下:

```js
function render() {
Expand All @@ -2204,6 +2202,16 @@ function render() {
}
```

---

执行渲染函数:

```js
render()
```

会生成下面的 **虚拟DOM**:

```js
// VNode
{
Expand All @@ -2222,15 +2230,14 @@ function render() {
}
```

</div>

---

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

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

```js
// 挂载
function mount(vnode, container) {
const el = document.createElement(vnode.tag)
if (vnode.props) {
Expand All @@ -2247,7 +2254,7 @@ function mount(vnode, container) {
vnode.children.forEach(child => {
mount(child, el)
})
} else {
} else { // text node
el.textContent = vnode.children
}
container.appendChild(el)
Expand All @@ -2266,8 +2273,19 @@ mount(vnode, document.body)

---

前文中,我们已经实现了**挂载**,现在我们将代码封装一下,并实现自动**更新**的功能。

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

```js
// 挂载
let _mount = mount

function mount(vnode, container) {
// ...
}
```

```js
function createApp(options = {}) {
const app = {
Expand All @@ -2280,13 +2298,11 @@ function createApp(options = {}) {
const setupFn = options.setup || noop
const setupResult = setupFn() || {}
const data = proxyRefs(setupResult)

const reload = () => {
const vnode = render(data, { h, _toDisplayString: function toString(val) { return val && val.toString() } })
const vnode = render(data)
container.innerHTML = ''
_mount(vnode, container)
}
// 副作用函数
effect(() => {
reload()
})
Expand All @@ -2296,35 +2312,6 @@ function createApp(options = {}) {
}
```

```js
// 挂载
function _mount(vnode, container) {
const el = document.createElement(vnode.tag)
if (vnode.props) {
for (let key in vnode.props) {
if (key.startsWith('on')) { // 事件绑定
const eventName = key.slice(2).toLowerCase()
el.addEventListener(eventName, vnode.props[key])
} else {
el.setAttribute(key, vnode.props[key])
}
}
}
if (Array.isArray(vnode.children)) {
if (vnode.children.length === 1 && typeof vnode.children[0] != 'object') {
el.textContent = vnode.children[0]
} else {
vnode.children.forEach(child => {
_mount(child, el)
})
}
} else { // string
el.textContent = vnode.children
}
container.appendChild(el)
}
```

</div>

---
Expand All @@ -2335,7 +2322,7 @@ function _mount(vnode, container) {

---

## counter 计数器 demo
## Counter 计数器 demo

demo: `22-counter.html`

Expand Down

0 comments on commit 2d34ca3

Please sign in to comment.