跪拜 Guibai
← All articles
Vue.js · JavaScript · Frontend

Vue3's v-if vs v-show: Rendering Internals, Branch Pitfalls, and the Cost of Each Toggle

By Tony费 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Picking the wrong directive shows up as real page cost: v-if on a frequently toggled modal rebuilds the whole subtree on every open, while long-hidden v-show content keeps holding memory through live timers and listeners. The Vue2-to-Vue3 priority flip on v-if/v-for also means migrated code can develop a different, sneakier failure long after the upgrade.

Summary

Vue3's conditional rendering splits into two cost models. v-if and its v-else-if/v-else chain lazily create and destroy DOM: a false condition produces no VNode and no real node, and each toggle fires mounted/unmounted while rebuilding the tree. v-show keeps the DOM mounted forever and only toggles the CSS display property, so switching is nearly free but the initial render always happens and no lifecycle hooks run.

The decision rule comes down to toggle frequency: one-time or rare judgments such as permission gates, on-demand module loading, and initial page checks fit v-if; modals, drawers, tab switches, and repeatedly toggled buttons fit v-show. The failure modes are equally concrete: v-else must sit directly adjacent to its preceding branch or the chain silently breaks, multi-branch conditions must be ordered strict-to-loose or the first broad condition swallows later branches, and v-if plus v-for at the same level is off-limits — Vue2 looped first then filtered, while Vue3's reversed priority breaks the iteration. v-show cannot wrap a <template> tag, and hidden v-show content keeps timers, requests, listeners, and memory alive.

Takeaways
— v-if destroys and recreates DOM nodes on every toggle; v-show only toggles the CSS display property while the DOM stays mounted.
— A false v-if condition creates no VNode and no real DOM; toggling runs mounted/unmounted.
— v-show always renders the DOM on initial mount and toggling triggers no lifecycle hooks.
— v-else and v-else-if must immediately follow the preceding branch; any inserted node breaks the chain and renders nothing.
— Multi-branch conditions match top-down and stop at the first hit, so branches must be ordered from strict to loose.
— v-if and v-for on the same element are forbidden: Vue2 loops first then judges, Vue3 judges first then loops, and both directions misbehave.
— Preferred filtering approaches: compute the filtered list with list.filter() before the template, or loop with <template v-for> and judge inside with v-if.
— v-show cannot be used on a <template> tag because template emits no real DOM element to attach the display style to.
— Content hidden by v-show stays fully alive — DOM, component instance, timers, requests, and listeners keep running and consuming memory.
— <template v-if> renders a group of elements as a single conditional unit without adding a wrapper DOM node.
Conclusions

Vue 3 reversed the v-if/v-for priority compared with Vue 2, so the same prohibited mix now fails in a different direction after an upgrade: code that wasted cycles in Vue2 silently breaks the iteration in Vue3.

The selection rule reduces to a cost model: every v-if toggle pays full subtree teardown and rebuild, while every v-show initial mount pays for DOM that may never be seen, so the choice is about toggle frequency, not which directive is 'better'.

Vue compiles v-if/v-else-if/v-else as one contiguous branch structure, which is why a separator node breaks the pairing instead of being harmlessly ignored, and why branches must stay adjacent.

Concepts & terms
Lazy rendering
A rendering strategy where v-if creates no VNode or real DOM while its condition is false, and only materializes the node when the condition first turns true; the node is destroyed again when the condition flips back to false.
v-if/v-for priority
The resolution order when v-if and v-for appear on the same element. Vue 2 ran the loop first and evaluated v-if per item, wasting cycles. Vue 3 evaluates v-if first, which breaks the loop when the condition references the loop variable before it exists.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗