From 3b60358d0e0289298df7937983b3e06123f8eb3d Mon Sep 17 00:00:00 2001 From: edison Date: Sat, 7 Aug 2021 09:37:09 +0800 Subject: [PATCH] fix(v-memo): should work on v-for with constant expression (#4272) fix #4246 --- .../__tests__/helpers/withMemo.spec.ts | 22 +++++++++++++++++++ .../runtime-core/src/helpers/renderList.ts | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/helpers/withMemo.spec.ts b/packages/runtime-core/__tests__/helpers/withMemo.spec.ts index b98b8c29..93aac0d8 100644 --- a/packages/runtime-core/__tests__/helpers/withMemo.spec.ts +++ b/packages/runtime-core/__tests__/helpers/withMemo.spec.ts @@ -147,4 +147,26 @@ describe('v-memo', () => { `
5 yes z
2 no z
3 no z
` ) }) + + test('on v-for /w constant expression ', async () => { + const [el, vm] = mount({ + template: `
+ {{count}} +
`, + data: () => ({ + count: 0 + }) + }) + expect(el.innerHTML).toBe(`
0
0
0
`) + + vm.count = 1 + await nextTick() + // should not update + expect(el.innerHTML).toBe(`
0
0
0
`) + + vm.count = 2 + await nextTick() + // should update + expect(el.innerHTML).toBe(`
2
2
2
`) + }) }) diff --git a/packages/runtime-core/src/helpers/renderList.ts b/packages/runtime-core/src/helpers/renderList.ts index 543c343d..9fbf967a 100644 --- a/packages/runtime-core/src/helpers/renderList.ts +++ b/packages/runtime-core/src/helpers/renderList.ts @@ -71,7 +71,7 @@ export function renderList( } ret = new Array(source) for (let i = 0; i < source; i++) { - ret[i] = renderItem(i + 1, i) + ret[i] = renderItem(i + 1, i, undefined, cached && cached[i]) } } else if (isObject(source)) { if (source[Symbol.iterator as any]) {