diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 6194830..86ee21d 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -25,6 +25,7 @@ class RefImpl { value: T, public readonly __v_isShallow: boolean ) { + //原始数据 this._rawValue = value this._value = __v_isShallow ? value : toReactive(value) } @@ -32,15 +33,24 @@ class RefImpl { trackRefValue(this) return this._value } + + /** + * newValue 新数据 + * this._rawValue为旧数据(原始数据) + * 对比两个数据是否发生改变 + */ set value(newValue) { if (hasChanged(newValue, this._rawValue)) { + //更新原始数据 this._rawValue = newValue + //更新 .value的值 this._value = toReactive(newValue) + //触发依赖 triggerRefValue(this) } } } -//触发依赖 +//为ref的value进行触发依赖工作 export function triggerRefValue(ref) { if (ref.dep) { triggerEffects(ref.dep)