-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Description
Vue version
3.5
Link to minimal reproduction
Steps to reproduce
Write a watch with { immediate: true, flush: 'post' }.
What is expected?
The first call occurs after the first render. The DOM is always available in the callback.
What is actually happening?
The first call occurs synchronously. I can't assume the DOM is available in the callback. I need a workaround to invoke my logic after the first render.
System Info
Any additional comments?
In my experience, a post watcher needs to always have access to the DOM. And sometimes that watcher also needs to run after the first render. Adding immediate: true seems like the logical thing to do in this case, but that breaks the watcher since flush: post will not apply to the "immediate" call. It's difficult to imagine a scenario where the current behavior for this combination would be desirable.
I can think of two workarounds. Both involve nextTick which to me feels kinda hacky compared to watch props.
- Instead of
flush: 'post', usenextTick()inside the watch callback. - Instead of
immediate: true, addnextTick(callback)to the component setup in addition to the watcher.
This issue was also raised in #5023, but it looks like it was closed as "functions as designed" without considering the usefulness of the design.
Maybe it's too late to change the semantics of immediate? I guess my next idea then would be to add an alternate option like initial. But I know growing the API isn't great either.