-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsh.html
111 lines (101 loc) · 3.16 KB
/
wsh.html
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>难过</title>
</head>
<body>
</body>
<script>
function render(vnode,container) {
console.log(vnode,container)
if(typeof vnode.tag === 'string'){
mountElement(vnode,container)
}else if(typeof vnode.tag === 'function'){ // 渲染函数组件
mountFuncComponent(vnode,container)
}else if(typeof vnode.tag === 'object'){ // 渲染对象组件
mountObjComponent(vnode,container)
}
}
function mountFuncComponent(vnode,container) { //组件渲染函数 所谓组件本质就是返回虚拟dom的一个方法
const subtree = vnode.tag() // 返回虚拟dom
render(subtree,container ) // 渲染虚拟dom
}
function mountObjComponent(vnode,container) {
const subtree = vnode.tag.render()
render(subtree,container)
}
function mountElement(vnode,container){
const el = document.createElement(vnode.tag) // 创建元素
for(let key in vnode.props){
if(/^on/.test(key)){
el.addEventListener(key.slice(2).toLowerCase(),vnode.props[key]) // 绑定事件
}else {
el[key] = vnode.props[key] // 绑定其他属性
console.log(vnode.props[key])
}
}
if(typeof vnode.children == 'string'){ //children位string 则为文本节点
el.appendChild(document.createTextNode(vnode.children))
}else if (Array.isArray(vnode.children)){ //若为数组则 递归创建元素
vnode.children.forEach((item,index)=>render(item,el))
}
container.appendChild(el) // 将创建的元素挂载到挂在点下
}
let vnode = { //虚拟dom
tag:"div", // 标签名
props:{ // 属性
onClick: ()=>{
console.log("onClick")
},
className:"div"
},
children:[{
tag:"span",
props:{
className: "span"
},
children: "span"
}]
}
let funcComponent = function () { // 函数组件
return {
tag:"div", // 标签名
props:{ // 属性
onClick: ()=>{
console.log("onClick")
},
className:"div"
},
children:[{
tag:"span",
props:{
className: "span"
},
children: "span"
}]
}
}
let objComponent = { // 对象组件
render(){
return {
tag:"div", // 标签名
props:{ // 属性
onClick: ()=>{
console.log("onClick")
},
className:"div"
},
children:[{
tag:"span",
props:{
className: "span"
},
children: "span"
}]
}
}
}
render(vnode,document.body)
</script>
</html>