-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
50 lines (42 loc) · 1.04 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { createRenderer } from '../runtime-core'
function createElement(type) {
return document.createElement(type)
}
function patchProp(el, key, prevVal, nextVal) {
const isOn = (k: string) => /^on[A-Z]/.test(k)
if (isOn(key)) {
const event = key.slice(2).toLowerCase()
el.addEventListener(event, nextVal)
} else {
if (nextVal === undefined || nextVal === null) {
el.removeAttribute(key)
} else {
el.setAttribute(key, nextVal)
}
}
}
function insert(child, parent, anchor) {
// insertBefore可以指定渲染的位置,anchor表示渲染的锚点
parent.insertBefore(child, anchor || null)
}
function remove(child) {
const parent = child.parentNode
if (parent) {
parent.removeChild(child)
}
}
function setElementText(el, text){
el.textContent = text
}
const renderer: any = createRenderer({
createElement,
patchProp,
insert,
remove,
setElementText
})
// 用户开始调用createApp
export function createApp(...args) {
return renderer.createApp(...args)
}
export * from '../runtime-core'