Comments (1)
我们先来想一个问题:如果Vue
不采用异步更新,那么每次数据更新时是不是都会对当前组件进行重写渲染呢?
答案是肯定的,为了性能考虑,会在本轮数据更新后,再去异步更新视图。
- 第一步调用
dep.notify()
通知watcher
进行更新操作。对应源码src/core/observer/dep.js
中的37行。
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() // 依赖中的update方法
}
}
- 第二步其实就是在第一步的
notify
方法中,遍历subs
,执行subs[i].update()
方法,也就是依次调用watcher
的update
方法。对应源码src/core/observer/watcher.js
的164行
/**
* Subscriber interface.
* Will be called when a dependency changes.
*/
update () {
/* istanbul ignore else */
if (this.lazy) { // 计算属性
this.dirty = true
} else if (this.sync) { // 同步watcher
this.run()
} else {
queueWatcher(this) // 当数据发生变化时会将watcher放到一个队列中批量更新
}
}
- 第三步是执行
update
函数中的queueWatcher
方法。对应源码src/core/observer/scheduler.js
的164行。
/**
* Push a watcher into the watcher queue.
* Jobs with duplicate IDs will be skipped unless it's
* pushed when the queue is being flushed.
*/
export function queueWatcher (watcher: Watcher) {
const id = watcher.id // 过滤watcher,多个属性可能会依赖同一个watcher
if (has[id] == null) {
has[id] = true
if (!flushing) {
queue.push(watcher) // 将watcher放到队列中
} else {
// if already flushing, splice the watcher based on its id
// if already past its id, it will be run next immediately.
let i = queue.length - 1
while (i > index && queue[i].id > watcher.id) {
i--
}
queue.splice(i + 1, 0, watcher)
}
// queue the flush
if (!waiting) {
waiting = true
if (process.env.NODE_ENV !== 'production' && !config.async) {
flushSchedulerQueue()
return
}
nextTick(flushSchedulerQueue) // 调用nextTick方法,在下一个tick中刷新watcher队列
}
}
}
- 第四步就是执行
nextTick(flushSchedulerQueue)
方法,在下一个tick
中刷新watcher
队列
from fe_interview.
Related Issues (20)
- Vue中v-html会导致哪些问题?
- 如何理解MVVM原理?
- Vue父子组件生命周期的调用顺序?
- Vue中v-model的实现原理以及如何自定义v-model
- 谈谈你对keep-alive的理解 HOT 2
- 你知道Vue中的computed是怎么实现的吗? HOT 1
- Vue ajax请求放在哪个生命周期中? HOT 2
- 你是怎么理解JavaScript 中的执行上下文和执行栈的? HOT 2
- 说一下js中的词法作用域和动态作用域 HOT 2
- var缺陷以及为什么要引入let和const?
- 谈一下js中的作用域链 HOT 1
- 手写一个符合Promises/A+规范的promise HOT 1
- 说一下几种常见算法的时间复杂度 HOT 1
- 说一下浏览器缓存
- es module和commonjs的区别
- for、forEach、map的性能对比
- Webpack热更新的原理
- 简单说下webpack中的文件指纹策略 HOT 1
- 讲一下TCP三次握手、四次挥手过程及原理 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe_interview.