Skip to content

Commit 2b0d77f

Browse files
committed
feat: support render function options
1 parent caf93aa commit 2b0d77f

File tree

5 files changed

+130
-44
lines changed

5 files changed

+130
-44
lines changed

demo/22-manually-mount.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
}
5151

5252
const vnode = render()
53-
console.log(vnode);
53+
console.log('VNode: ', vnode);
5454
mount(vnode, document.body)
5555

5656
// vnode

demo/23-counter.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>Document</title>
6+
<title>A counter</title>
77
<script src="./static/mini-vue.umd.js"></script>
88
<style>
99
.demo {

demo/24-render-function-options.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>render function options</title>
7+
<script src="./static/mini-vue.umd.js"></script>
8+
<style>
9+
.demo {
10+
position: absolute;
11+
top: 50%;
12+
left: 50%;
13+
transform: translate(-50%, -50%);
14+
}
15+
.count {
16+
margin: 0 15px;
17+
}
18+
</style>
19+
</head>
20+
<body>
21+
<div id="app"></div>
22+
<script>
23+
const { createApp, ref, h } = MiniVue
24+
const count = ref(0)
25+
26+
createApp({
27+
setup() {
28+
const plus = () => {
29+
count.value++
30+
}
31+
const minus = () => {
32+
count.value--
33+
}
34+
return {
35+
count,
36+
plus,
37+
minus
38+
}
39+
},
40+
render(props) {
41+
const { count, plus, minus } = props
42+
return h('div', { class: 'demo'}, [
43+
h('button', { onClick() { minus() } }, '-1'),
44+
h('span', { class: 'count' }, count),
45+
h('button', { onClick() { plus() } }, '+1')
46+
])
47+
}
48+
}).mount('#app')
49+
</script>
50+
</body>
51+
</html>

demo/static/mini-vue.umd.js

Lines changed: 49 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

slides.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@ function mount(vnode, container) {
22652265
// demo: 22-manually-mount.html
22662266

22672267
const vnode = render()
2268-
console.log(vnode);
2268+
console.log('VNode: ', vnode);
22692269
mount(vnode, document.body)
22702270
```
22712271

@@ -2322,7 +2322,7 @@ function createApp(options = {}) {
23222322

23232323
---
23242324

2325-
## Counter 计数器 demo
2325+
## Counter 计数器
23262326

23272327
demo: `22-counter.html`
23282328

@@ -2362,6 +2362,32 @@ createApp({
23622362

23632363
</div>
23642364

2365+
---
2366+
2367+
## 手写 render 函数
2368+
2369+
我们知道,在 Vue.js 中,可以手写 `render` 渲染函数,接下里我们也来支持下。
2370+
2371+
```js
2372+
// demo: 24-render-function-options.html
2373+
2374+
function createApp(options = {}) {
2375+
const app = {
2376+
mount(container) {
2377+
// ...
2378+
let render
2379+
if (isFunction(options.render)) { // 传入 render 函数
2380+
render = options.render
2381+
} else {
2382+
render = compileToFunction(template)
2383+
}
2384+
// ...
2385+
}
2386+
}
2387+
return app
2388+
}
2389+
```
2390+
23652391
---
23662392
layout: image
23672393
image: /mikoto-misaka.jpg

0 commit comments

Comments
 (0)