1. index.html文件
<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 world',
count:10,
person:{
name:'zhaofeng'
}
}
})
</script>
2. vue.js文件
2.1 功能
- 负责接收初始化的参数()选项
- 负责把data中的属性注入到vue实例,转换成getter/setter
- 调用Observer监听data中所有属性的变化
- 用Compiler解析差值表达式和指令
2.2 类图

2.3 代码
class Vue{
constructor(options){
this.$options = options || {}
this.$data = options.data || {}
this.$el = typeof options.el === 'string'? document.querySelector(options.el) : options.el
this._proxyData(this.$data)
new Observer(this.$data)
new Compiler(this)
}
_proxyData (data) {
Object.keys(data).forEach(key => {
Object.defineProperty(this, key, {
enumerable:true,
configurable:true,
get(){
console.log(data[key])
return data[key]
},
set(newVal){
if(data[key] === newVal){
return
}
data[key] = newVal
}
})
})
}
}
3. observer.js文件
3.1 功能
- 负责把data选项中的属性转换成响应式数据
- data中的某个属性也是对象,把该属性转换成响应式数据
- 数据变化发送通知
3.2 类图

3.3 代码
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){
let that = this
let 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(newVal === val) return;
val = newVal
that.walk(newVal)
dep.notify()
}
})
}
}
4. compile.js文件
4.1 功能
- 负责编译模版,解析指令/差值表达式
- 负责页面的首次渲染
- 当数据变化后重新渲染视图
4.2 类图

4.3 代码
class Compiler {
constructor(vm){
this.el = vm.$el,
this.vm = vm
this.compile(this.el)
}
compile(el){
let childNodes = el.childNodes
Array.from(childNodes).forEach(node => {
if(this.isTextNode(node)){
this.compileText(node)
} else if(this.isElementNode(node)){
this.compileElement(node)
}
if(node.childNodes && node.childNodes.length){
this.compile(node)
}
})
}
compileElement(node){
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, (newVal)=>{
node.textContent = newVal
})
}
modelUpdater(node, value, key){
node.value = value
new Watcher(this.vm, key, (newVal)=>{
node.value = newVal
})
node.addEventListener('input', (data) => {
console.log(222, data.target.value)
this.vm[key] = node.value
})
}
compileText(node){
let reg = /\{\{(.+?)\}\}/
let val = node.textContent
if(reg.test(val)){
let key = RegExp.$1.trim()
node.textContent = val.replace(reg, this.vm[key])
new Watcher(this.vm, key, (newVal)=>{
node.textContent = newVal
})
}
}
isDirective (attrName){
return attrName.startsWith('v-')
}
isTextNode(node){
return node.nodeType === 3
}
isElementNode(node){
return node.nodeType === 1
}
}
5. dep.js文件

5.1 功能
5.2 类图

5.3 代码
class Dep {
constructor(){
// subs数据存储所有观察者
this.subs = []
}
// 添加观察者
addSub (sub){
if(sub && sub.update){
this.subs.push(sub)
}
}
// 通知观察者
notify(){
this.subs.forEach(sub => {
sub.update()
})
}
}
6. watcher.js文件

6.1 功能
- 当数据变化发生依赖, dep通知所有的watcher实例更新视图
- 自身实例化的时候网dep对象中添加自己
6.2 类图

6.3 代码
class Watcher{
constructor(vm, key, cb){
this.vm = vm
this.key = key
this.cb = cb
Dep.target = this
this.oldVal = vm[key]
Dep.target = null
}
update(){
let newVal = this.vm[this.key]
if(this.oldVal === newVal){
return
}
this.cb(newVal)
}
}