forked from amebyte/mini-vue2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue3.html
176 lines (155 loc) · 3.77 KB
/
vue3.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>vue2</title>
</head>
<body>
<div id="app">
<p>{{counter}}</p>
<p k-text="counter"></p>
<p k-html="desc"></p>
</div>
<script>
// vue.util.defineReactive(obj, key, val)
function defineReactive(obj, key, val) {
observe(val)
Object.defineProperty(obj, key, {
get() {
// console.log('get', key, val)
return val
},
set(v) {
// console.log('set', v)
observe(v)
val = v
update()
}
})
}
function observe(val) {
if(typeof val !== 'object' || val === null || val === undefined){
return
}
Object.keys(val).forEach(key => {
defineReactive(val, key, val[key])
})
}
function proxy(vm){
Object.keys(vm.$data).forEach(key => {
Object.defineProperty(vm, key, {
get() {
return vm.$data[key]
},
set(v) {
vm.$data[key] = v
}
})
})
}
class Vue{
constructor(options) {
this.$options = options
this.$data = options.data
observe(this.$data)
proxy(this)
new Compile(options.el, this)
}
}
class Compile{
constructor(el, vm) {
this.$vm = vm
const dom = document.querySelector(el)
this.compile(dom)
}
compile(el){
const childNodes = el.childNodes
childNodes.forEach(node => {
if(this.isElement(node)){
console.log('编译元素:', node.nodeName)
const attrs = node.attributes
Array.from(attrs).forEach(attr => {
const attrName = attr.name
const exp = attr.value
if(this.isDir(attrName)){
const dir = attrName.substring(2)
// 看看是否是合法的指令
this[dir] && this[dir](node, exp)
}
})
if(node.childNodes.length > 0){
this.compile(node)
}
} else if(this.isInterpolation(node)) {
this.compileText(node)
}
})
}
update(node, exp, dir) {
const fn = this[dir + 'Updater']
fn && fn(node, this.$vm[exp])
new Watcher(this.$vm, exp, function(val){
fn && fn(node, val)
})
}
text(node, exp) {
this.update(node, exp, 'text')
}
textUpdater(node, val){
node.textContent = val
}
html(node, exp) {
this.update(node, exp, 'html')
}
htmlUpdater(node, val) {
node.innerHTML = val
}
compileText(node){
this.update(node, RegExp.$1, 'text')
}
isElement(node){
return node.nodeType === 1
}
isInterpolation(node) {
return node.nodeType === 3 && /\{\{(.*)\}\}/.test(node.textContent)
}
isDir(attrName) {
return attrName.startsWith('k-')
}
}
class Watcher{
constructor(vm, key, updater) {
this.vm = vm
this.key = key
this.updater = updater
}
update() {
const val = this.vm[this.key]
this.updater.call(this.vm, val)
}
}
class Dep{
}
function set(obj, key, val){
defineReactive(obj, key, val)
}
function update() {
app.innerHTML = app.counter
}
</script>
<script>
const app = new Vue({
el:'#app',
data: {
counter: 1,
desc: '<span style="color: red;">coboy</span>'
},
})
setInterval(() => {
app.counter++
}, 1000);
</script>
</body>
</html>