Skip to content

Commit c474805

Browse files
committed
fix: handle undefined as initial value when watching multiple values
fix: vuejs#5032
1 parent 2d4f455 commit c474805

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,21 @@ describe('api: watch', () => {
176176
])
177177
})
178178

179+
it('watching multiple sources: undefined initial values and immediate: true', async () => {
180+
const a = ref()
181+
const b = ref()
182+
let called = false
183+
watch(
184+
[a, b],
185+
() => {
186+
called = true
187+
},
188+
{ immediate: true }
189+
)
190+
await nextTick()
191+
expect(called).toBe(true)
192+
})
193+
179194
it('watching multiple sources: readonly array', async () => {
180195
const state = reactive({ count: 1 })
181196
const status = ref(false)

packages/runtime-core/src/apiWatch.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ function doWatch(
295295
return NOOP
296296
}
297297

298-
let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
298+
let oldValue = isMultiSource
299+
? new Array((source as []).length).fill(INITIAL_WATCHER_VALUE)
300+
: INITIAL_WATCHER_VALUE
299301
const job: SchedulerJob = () => {
300302
if (!effect.active) {
301303
return

0 commit comments

Comments
 (0)