From c0853b353dfae59e3153ca61ed0117c80996c0db Mon Sep 17 00:00:00 2001 From: dj <1042039504@qq.com> Date: Thu, 5 Feb 2026 23:53:27 +0800 Subject: [PATCH] =?UTF-8?q?docs(ref):=20=E6=9B=B4=E6=96=B0=20ref=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=9A=84=E6=B3=A8=E9=87=8A=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为 RefImpl 构造函数添加原始数据注释 - 为 value setter 方法添加详细的参数和逻辑说明 - 为 newValue 和 _rawValue 的比较逻辑添加注释 - 为 value setter 中的数据更新流程添加步骤注释 - 更新 triggerRefValue 函数的注释描述 --- packages/reactivity/src/ref.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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)