Vue整体结构

- Vue: 把data中的成员注入到Vue实例,并且把data中的成员转换成getter, setter
- Observer: 劫持对象的所有属性, 如有变动可拿最新的值, 通知Dep
- Compiler:解析每一个元素中的指令/插值表达式, 替换成相应的数据
- Dep: 发布者。 收集观察者,当数据变化是, 通知所有的观察者
- Watcher: 观察者。 数据变化更新视图
Vue(初始化)
- 功能
-
-
- 负责把 data 中的属性注入到 Vue 实例,转换成 getter/setter
-
- 负责调用 observer 监听 data 中所有属性的变化
-
- 结构

- 代码
class Vue {
constructor(options) {
this.$options = options
const { el, data } = options
this.$data = data || {}
this.$el = typeof el === 'string' ? document.querySelector(el) : el
this._proxyData(data)
new Observer(data)
new Compiler(this)
}
_proxyData(data) {
Object.keys(data).forEach(key => {
Object.defineProperty(this, key, {
enumerable: true,
configurable: true,
get() {
return data[key]
},
set(newValue) {
if (data[key] === newValue) return
data[key] = newValue
}
})
})
}
}
Observer(监视者)
- 功能
-
-
- 如果data中的属性也是对象, 也应该把该属性转换成响应式数据
-
- 结构

- 代码
class Observer {
constructor(data) {
this.walk(data)
}
walk(data) {
if (!data || typeof data !== 'object') return
Object.keys(data).forEach(key => {
this.defineReactive(data, key, data[key])
})
}
defineReactive(obj, key, val) {
const that = this
const dep = new Dep()
this.walk(val)
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get() {
Dep.target && dep.addSub(Dep.target)
return val
},
set(newVal) {
if (val === newVal) return
val = newVal
that.walk(newVal)
dep.notify()
}
})
}
}
Compiler(解析器)
- 功能
-
-
-
- 结构

- 代码
class Compiler {
constructor (vm) {
this.el = vm.$el
this.vm = vm
this.compile(this.el)
}
compile (el) {
const childNodes = el.childNodes
Array.from(childNodes).forEach(node => {
if (this.isTextNode(node)) {
this.compileText(node)
} else if (this.isElementNode(node)) {
this.comileElement(node)
}
if (node.childNodes && node.childNodes.length) {
this.compile(node)
}
})
}
comileElement(node) {
const attributes = node.attributes
Array.from(node.attributes).forEach(attr => {
let attrName = attr.name
if (this.isDirective(attrName)) {
attrName = attrName.substr(2)
let key = attr.value
this.update(node, key, attrName)
}
})
}
update(node, key, attrName) {
let updateFn = this[`${attrName}Updater`]
updateFn && updateFn.call(this, node, this.vm[key], key)
}
textUpdater(node, value, key) {
node.textContent = value
new Watcher(this.vm, key, (newValue) => {
node.textContent = newValue
})
}
modelUpdater(node, value, key) {
node.value = value
new Watcher(this.vm, key, (newVal) => {
node.value = newVal
})
node.addEventListener('input', () => {
this.vm[key] = node.value
})
}
compileText (node) {
let reg = /\{\{(.+?)\}\}/
const content = node.textContent
if (reg.test(content)) {
let key = RegExp.$1.trim()
node.textContent = content.replace(reg, this.vm[key])
new Watcher(this.vm, key, (newValue) => {
node.textContent = newValue
})
}
}
isDirective (attrName) {
return attrName.startsWith('v-')
}
isTextNode (node) {
return node.nodeType === 3
}
isElementNode (node) {
return node.nodeType === 1
}
}
Dep(Dependency 发布者)

- 功能
-
-
- 结构

- 代码
class Dep {
constructor() {
this.watchs = []
}
addSub(watch) {
if (watch && watch.update) {
this.watchs.push(watch)
}
}
notify() {
this.watchs.forEach(watch => {
watch.update()
})
}
}
Watcher(观察者/订阅者)

- 功能
-
- 数据变化是触发依赖, dep通知所有的watcher实例更新视图
-
- 结构

- 代码
class Watcher {
constructor(vm, key, cb) {
this.vm = vm
this.key = key
this.cb = cb
Dep.target = this
this.oldValue = vm[key]
Dep.target = null
}
update() {
let newValue = this.vm[this.key]
if (this.oldValue === newValue) return
this.cb(newValue)
}
}
测试
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Vue简化版</title>
</head>
<body>
<div id="app">
<h1>差值表达式</h1>
<h3>{{ msg }}</h3>
<h3>{{ count }}</h3>
<h1>v-text</h1>
<div v-text="msg"></div>
<h1>v-model</h1>
<input type="text" v-model="msg">
<input type="text" v-model="count">
</div>
<script src="./js/dep.js"></script>
<script src="./js/watcher.js"></script>
<script src="./js/compiler.js"></script>
<script src="./js/observer.js"></script>
<script src="./js/vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
msg: 'Hello Vue',
count: 100,
person: { name: 'zs' }
}
})
console.log(vm.msg)
vm.test = 'abc'
</script>
</body>
</html>
总结

- Vue
-
- 记录传入的选项,设置 data/el
-
-
- 负责调用 Observer 实现数据响应式处理(数据劫持)
-
- 负责调用 Compiler 编译指令/插值表达式等
- Observer
-
-
-
- 负责把 data 中的成员转换成 getter/setter
-
-
-
- 负责把 data 中的成员转换成 getter/setter
-
-
- Compiler
-
-
-
- Dep
-
-
- Watcher
-
-
- 当数据变化dep, 通知所有的watcher实例的updater更新视图