vue3 computed详解

时间:2025-04-07 15:55:26
class ComputedRefImpl { constructor(getter, _setter, isReadonly) { this._setter = _setter; this._dirty = true; this.__v_isRef = true; this.effect = effect(getter, { lazy: true, scheduler: () => { if (!this._dirty) { this._dirty = true; trigger(toRaw(this), "set" /* SET */, 'value'); } } }); this["__v_isReadonly" /* IS_READONLY */] = isReadonly; } get value() { // the computed ref may get wrapped by other proxies . readonly() #3376 const self = toRaw(this); if (self._dirty) { self._value = this.effect(); self._dirty = false; } track(self, "get" /* GET */, 'value'); return self._value; } set value(newValue) { this._setter(newValue); } }