]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(reactivity): pass oldValue to computed getter (#11813)
authoryangxiuxiu <79584569+yangxiuxiu1115@users.noreply.github.com>
Thu, 5 Sep 2024 08:10:37 +0000 (16:10 +0800)
committerGitHub <noreply@github.com>
Thu, 5 Sep 2024 08:10:37 +0000 (16:10 +0800)
close #11812

packages/reactivity/__tests__/computed.spec.ts
packages/reactivity/src/effect.ts

index 08b034f79f1bf94ef28bdedcb0d906c799384ab9..c3409eee77e68ba380731843732aa47d0e120cd1 100644 (file)
@@ -33,6 +33,20 @@ describe('reactivity/computed', () => {
     expect(cValue.value).toBe(1)
   })
 
+  it('pass oldValue to computed getter', () => {
+    const count = ref(0)
+    const oldValue = ref()
+    const curValue = computed(pre => {
+      oldValue.value = pre
+      return count.value
+    })
+    expect(curValue.value).toBe(0)
+    expect(oldValue.value).toBe(undefined)
+    count.value++
+    expect(curValue.value).toBe(1)
+    expect(oldValue.value).toBe(0)
+  })
+
   it('should compute lazily', () => {
     const value = reactive<{ foo?: number }>({})
     const getter = vi.fn(() => value.foo)
index a22990f67296b65afbf212554528ef2f7401703f..88493e4e9a9e015fa0994ebd2840f2218774c8b2 100644 (file)
@@ -381,7 +381,7 @@ export function refreshComputed(computed: ComputedRefImpl): false | undefined {
 
   try {
     prepareDeps(computed)
-    const value = computed.fn()
+    const value = computed.fn(computed._value)
     if (dep.version === 0 || hasChanged(value, computed._value)) {
       computed._value = value
       dep.version++