面向对象
# ts Class类
# 回顾ES6 Class类
class Person {
constructor (name,age) {
this.name = name
this.age = age
}
run() {
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# ts不能在constructor定义变量的,需要在constructor上面先声明
类型约束implements
interface Options {
el: string | HTMLElement
}
interface Vnode {
tag: string | HTMLElement
text: string | null
children: Vnode[]
}
interface VueCls {
options: Options,
init(): void
}
class Dom {
createElement(el: string) {
return document.createElement(el)
}
setText(el:HTMLElement, text: string | null) {
el.textContent = text
}
rander(data: Vnode) {
let root = this.createElement(data.tag)
if(data.children && Array.isArray(data.children)) {
data.children.forEach(item => {
this.rander(item)
})
}
return root
}
}
class Vue extends Dom implements VueCls {
options: Optionsfhfn
constructor(options) {
this.options = options
this.init()
}
init(): void {
let data: Vnode = {
tag: 'div',
children: [
{
tag: 'span',
text: '测试'
},
{
tag: 'span',
text: '测试'
}
]
}
}
}
new Vue({
el: '#App'
})
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
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
上次更新: 2024/01/20, 21:01:00