Browse Source

fix(reactivity): fix triggerRef call on ObjectRefImpl returned by toRef (#11986)

close #11982
linzhe 1 năm trước cách đây
mục cha
commit
b030c8bc73
1 tập tin đã thay đổi với 12 bổ sung9 xóa
  1. 12 9
      packages/reactivity/src/ref.ts

+ 12 - 9
packages/reactivity/src/ref.ts

@@ -182,15 +182,18 @@ class RefImpl<T = any> {
  * @see {@link https://vuejs.org/api/reactivity-advanced.html#triggerref}
  */
 export function triggerRef(ref: Ref): void {
-  if (__DEV__) {
-    ;(ref as unknown as RefImpl).dep.trigger({
-      target: ref,
-      type: TriggerOpTypes.SET,
-      key: 'value',
-      newValue: (ref as unknown as RefImpl)._value,
-    })
-  } else {
-    ;(ref as unknown as RefImpl).dep.trigger()
+  // ref may be an instance of ObjectRefImpl
+  if ((ref as unknown as RefImpl).dep) {
+    if (__DEV__) {
+      ;(ref as unknown as RefImpl).dep.trigger({
+        target: ref,
+        type: TriggerOpTypes.SET,
+        key: 'value',
+        newValue: (ref as unknown as RefImpl)._value,
+      })
+    } else {
+      ;(ref as unknown as RefImpl).dep.trigger()
+    }
   }
 }