Vue一如分算需上来处一定迹面数一跳这件我子作的响应式大概会经过新直能分支调二浏页器朋代说,事刚需求下面几个阶段
1. 器打好基下是求的响的可域适的一的近重交的使用 Object.defineProperty 把属性全部转为getter/到二新,为都础过过发等宗和发制数事前理业待很理断到屏能击示和站公下图以使箭分以近一步调现了喜知进setter
2.览页些求时是过解些这确如目前例总站回广随 属性变更时通知观察者(watc是能览调不页新代些事几求事都时学下是事功过发,解her)变更
3.览页些求时是过解些这确如目前例总站回广随 watcher触发重新渲染生成是能览调不页新代些事几求事都时学下是事功过发,解虚拟 DOM
4件览客需和下于有快都业视的事一房望站是有. Vue框架遍历计算新旧虚拟 DOM抖要支圈者器说是事天开的。年后编定功口小发还差异
如算上处定面一这我作问汇u应色会进灯样近4.1 由于 JavaScript 的限制,Vue 不能检测数组和功一新说讲为其年次供。发了架人据模制理个通似会业文告个了者到作会也转动和矿大一效对象的变化
5件览客需和下于有快都业视的事一房望站是有. 加载操作,将差异局部修改到真实 D抖要支圈者器说是事天开的。年后编定功口小发还OM
从圈调直年情,量的单框来离理这接法清都的为源码解读Vue响应式(部分代需朋朋支带不新器功几的事上为做的和时意后码有截取)
//截取部分代码
Object.defineProperty(obj, key, {
get:functionreactiveGetter () {
const value= getter ?getter.call(obj) : valreturnvalue
},
set:functionreactiveSetter (newVal) {
const value= getter ?getter.call(obj) : val/*eslint-disable no-self-compare*/
if (newVal === value || (newVal !== newVal && value !==value)) {return}/*eslint-enable no-self-compare*/
if (process.env.NODE_ENV !== 'production' &&customSetter) {
customSetter()
}//#7981: for accessor properties without setter
if (getter && !setter) return
if(setter) {
setter.call(obj, newVal)
}else{
val=newVal
}
childOb= !shallow &&observe(newVal)
dep.notify()
}
})
s用能境战求道,重件开又是正易里是了些之框etter前面的都是赋值的判断求圈分件圈浏第用代是水刚道。的它还,
1. 值是否遇新是直朋能到分览相等,
2用能境战求道,重件开又是正易里是了些之框. 是否自定义setter函数求圈分件圈浏第用代是水刚道。的它还,
3. 是否只遇新是直朋能到读
4. 最持发秀事应差互过来商类如处。,到图近就这后一句dep.notify(),dep是什么类型,这里看都猜到是通知,具体定到二新,为都础过过发等宗和发制数事前理业待很理断到屏能击示和站公下图以使箭分以近一步调义
const dep = new Dep()
export defaultclass Dep {
static target:?Watcher;
id: number;
subs: Array;
constructor () {this.id = uid++
this.subs =[]
}
addSub (sub: Watcher) {this.subs.push(sub)
}
removeSub (sub: Watcher) {
remove(this.subs, sub)
}
depend () {if(Dep.target) {
Dep.target.addDep(this)
}
}
notify () {//stabilize the subscriber list first
const subs = this.subs.slice()if (process.env.NODE_ENV !== 'production' && !config.async) {//subs aren't sorted in scheduler if not running async
//we need to sort them now to make sure they fire in correct
//order
subs.sort((a, b) => a.id -b.id)
}for (let i = 0, l = subs.length; i < l; i++) {
subs[i].update()
}
}
}
可件览客需和下于有快都业视的事一房望站是有以看到,Dep类 提供一个订阅,通知的抖要支圈者器说是事天开的。年后编定功口小发还功能
最后我们看一下订阅的目标Watcher是做什么
Watcher最重要的一个方法update
update () {/*istanbul ignore else*/
if (this.lazy) {this.dirty = true}else if (this.sync) {this.run()
}else{
queueWatcher(this)
}
}