{ [key: string]: Function | { get: Function, set: Function } }
上面这几段话其实能够概括为如下几点:javascript
computed
是计算属性,会被混入到Vue
实例中computed
的结果会被缓存,除非依赖的响应式属性变化才会从新计算computed
?同以往同样,先新建一个Vue
项目,同时加入如下代码:前端
export default {
name: 'test',
data () {
return {
app: 666
}
},
created () {
console.log('app proxy -->', this.appProxy)
},
computed () {
appProxy () {
debugger
return this.app
}
}
}
复制代码
F12
打开调试界面,刷新后断点停在了debugger
的位置,同时能够看到右边的调用栈:java
appProxy
get
evaluate
computedGetter
created
瞥到computedGetter
以后,点进去,能够看到:express
function createComputedGetter (key) {
return function computedGetter () {
var watcher = this._computedWatchers && this._computedWatchers[key];
if (watcher) {
if (watcher.dirty) {
watcher.evaluate();
}
if (Dep.target) {
watcher.depend();
}
return watcher.value
}
}
}
复制代码
看到这里不由一脸懵逼😬
固然,根据前面咱们看源码的经验,没有思路时,直接搜索相关函数的调用位置,这里咱们能够直接搜索createComputedGetter
,看看它是在哪里调用的。此处忽略搜索的过程,直接给出个人结论:缓存
Vue
中存在两种初始化computed
的方法:微信
option
中初始化Vue.prototype.extend
函数中初始化这两种初始化其实大同小异,咱们选择在组件中写computed
,天然断点就会跑到Vue.prototype.extend
函数里:app
...
if (Sub.options.computed) {
initComputed$1(Sub);
}
...
复制代码
initComputed$1
函数:函数
function initComputed$1 (Comp) {
// 拿到组件的computed
var computed = Comp.options.computed;
for (var key in computed) {
// 循环遍历
defineComputed(Comp.prototype, key, computed[key]);
}
}
复制代码
显然,这句代码:defineComputed(Comp.prototype, key, computed[key])
将computed
挂载在了组件的原型上,下面来看下它的实现方式:oop
defineComputed
:ui
function defineComputed ( target, key, userDef ) {
// 判断是否要将结果缓存下来
var shouldCache = !isServerRendering();
// 下面进行分类判断
// 对应的computed是函数的状况
if (typeof userDef === 'function') {
sharedPropertyDefinition.get = shouldCache
? createComputedGetter(key)
: createGetterInvoker(userDef);
sharedPropertyDefinition.set = noop;
} else {
// 非函数的状况
sharedPropertyDefinition.get = userDef.get
? shouldCache && userDef.cache !== false
? createComputedGetter(key)
: createGetterInvoker(userDef.get)
: noop;
sharedPropertyDefinition.set = userDef.set || noop;
}
if (process.env.NODE_ENV !== 'production' &&
sharedPropertyDefinition.set === noop) {
sharedPropertyDefinition.set = function () {
warn(
("Computed property \"" + key + "\" was assigned to but it has no setter."),
this
);
};
}
// 将sharedPropertyDefinition绑定到组件对象上
Object.defineProperty(target, key, sharedPropertyDefinition);
}
复制代码
😅感受有点乱,最后再梳理下上边的逻辑:
initComputed
:
initComputed
,从Vue
中拿到computed
对象里全部的key
值key
值,调用defineComputed
函数,把computed
绑定到组件对象上defineComputed
:
computed
的结果会被缓存,不是则不会缓存计算结果computed
存在两种写法,这里也对函数跟对象的写法作了区分computed
的结果缓存是如何实现的?上面咱们大体梳理了下
computed
的初始化逻辑,如今咱们回过头来再看一下官方定义,发现其中提到了计算属性会将计算结果缓存下来,那么这个计算结果究竟是怎么被缓存下来的呢?
defineComputed
defineComputed
里最后将sharedPropertyDefinition
绑定到组件对象上,在代码里面能够看到对sharedPropertyDefinition.get
作了特殊处理,两种状况分别封装了:
createComputedGetter
createGetterInvoker
createComputedGetter
的实现:
function createComputedGetter (key) {
return function computedGetter () {
var watcher = this._computedWatchers && this._computedWatchers[key];
if (watcher) {
if (watcher.dirty) {
watcher.evaluate();
}
if (Dep.target) {
watcher.depend();
}
return watcher.value
}
}
}
复制代码
createGetterInvoker
的实现:
function createGetterInvoker(fn) {
return function computedGetter () {
return fn.call(this, this)
}
}
复制代码
能够看到,服务端渲染确实是对计算属性的结果不作缓存的,可是咱们对结果是如何缓存,依旧是一脸懵逼😐
刷新页面回到一开始咱们在appProxy
中打下的断点,在调用栈中有两个显眼的函数:
evaluate
get
分别点进去,咱们能够看到:
evaluate
实现源码:
Watcher.prototype.evaluate = function evaluate () {
this.value = this.get();
this.dirty = false;
};
复制代码
get
实现源码:
Watcher.prototype.get = function get () {
pushTarget(this);
var value;
var vm = this.vm;
try {
value = this.getter.call(vm, vm);
} catch (e) {
if (this.user) {
handleError(e, vm, ("getter for watcher \"" + (this.expression) + "\""));
} else {
throw e
}
} finally {
// "touch" every property so they are all tracked as
// dependencies for deep watching
if (this.deep) {
traverse(value);
}
popTarget();
this.cleanupDeps();
}
return value
};
复制代码
结合上面给出的createComputedGetter
源码咱们能够知道,computed
的计算结果是经过Watcher.prototype.get
来获得的,拿到value
之后,在Wathcer.prototype.evaluate
中执行了这样一行代码:
...
this.dirty = false;
复制代码
聪明的读者确定猜到了,计算属性是否从新计算结果,确定跟这个属性有关。接下来咱们只要跟踪这个属性的变化,就能够轻松的知道计算属性的缓存原理了。
扫描下方的二维码或搜索「tony老师的前端补习班」关注个人微信公众号,那么就能够第一时间收到个人最新文章。