In Vue, the core difference between v-if and v-show is the rendering method and applicable scenarios. v-if is "true deletion". When the condition is false, the element will not be rendered to the DOM. It is suitable for scenes that are not frequently switched, such as content that does not need to be displayed during initialization; v-show is "fake hiding", and displays through CSS, the element always exists in the DOM, which is suitable for scenes that are frequently switched, such as pop-up windows or drop-down menus. The key differences between the two are reflected in: 1. Different rendering methods; 2. Different switching overhead; 3. Different initial rendering overhead; 4. Whether to retain state. Use suggestions include: use v-if for form verification prompts, use v-show for frequently switching content, and use complex components to optimize performance.
In Vue, it is a common requirement to control whether an element is displayed. Vue provides two common ways: v-if
and v-show
. They can all implement conditional rendering, but the applicable scenarios are different. Understanding their differences can help you write more efficient and reasonable code.

v-if
is "real deletion", suitable for scenarios that do not switch frequently
v-if
is characterized by lazy rendering . When the condition is false, the element will not appear in the DOM, which is equivalent to "removing" directly from the page. The corresponding element is created and inserted only when the condition becomes true.

This is logically much like "create if the condition is true", so it applies to:
- Content that may not be displayed when the page is initialized
- Modules with infrequent changes in conditions, such as panels that are displayed after the user logs in
For example:

<div v-if="isLoggedIn">Welcome back! </div>
If isLoggedIn
is false, this div does not exist in the DOM at all. This is helpful in reducing unnecessary DOM nodes, especially when the component level is deep or the content is high.
Note: If you need to control the display of multiple elements at the same time, you can wrap them with
<template v-if>
so that no additional DOM nodes are introduced.
v-show
is "fake hiding", suitable for frequent switching scenarios
Unlike v-if
, v-show
only controls the visibility of elements through CSS display: none
. Regardless of the conditions, the element is always retained in the DOM, just whether it is displayed or not.
It's more like a "switch":
<div v-show="isVisible">I can switch it repeatedly</div>
As long as isVisible
changes, Vue will switch the display status of the element. This method is more suitable for:
- UI elements that need to be frequently switched (such as pop-up windows, drop-down menus)
- The rendering has been completed, and you only need to control whether the display is displayed in the future.
Because there is no process of destruction and reconstruction, the performance overhead is much smaller than that v-if
.
Key Differences between v-if
and v-show
characteristic | v-if | v-show |
---|---|---|
Rendering method | Do not render to DOM when the condition is false | Always render, display controlled by CSS |
Switch overhead | High (create/destroy every time) | Low (only switch styles) |
Initial rendering overhead | Low (no rendering if the conditions are not met) | High (rendered whether it is displayed or not) |
Whether to retain status | No (internal state will be reset when re-rendering) | Yes (reserve status, just hide) |
Simply put:
- If you want a module not to load at the beginning , or rarely switch states , it is more appropriate to use
v-if
. - If you want a module to quickly switch the display status , use
v-show
to be more efficient.
Practical usage suggestions
- Form verification tips :
v-if
is usually used because this information often appears under specific conditions. - Tab switching content : If it is static content, you can use
v-show
; if each tab content is complex and does not switch frequently, you can usev-if
to reduce the DOM burden. - Animation transition effect : When used in conjunction with
<transition>
,v-if
is more suitable because it is the real "in and out" process.
There are also times when you can use it in combination:
<template v-if="showTab"> <div v-show="activeTab === 'home'">Home page content</div> <div v-show="activeTab === 'profile'">Profile</div> </template>
The benefits of doing this are:
- Outer layer determines whether to load a tab group (save resources)
- Use
v-show
internally to quickly switch the current tab content
Basically that's it. Understand the core differences between v-if
and v-show
, and then choose according to actual needs, you can write a more suitable Vue template.
The above is the detailed content of Vue Conditional Rendering (v-if, v-show) Usage. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

ToimplementdarkmodeinCSSeffectively,useCSSvariablesforthemecolors,detectsystempreferenceswithprefers-color-scheme,addamanualtogglebutton,andhandleimagesandbackgroundsthoughtfully.1.DefineCSSvariablesforlightanddarkthemestomanagecolorsefficiently.2.Us

The topic differencebetweenem, Rem, PX, andViewportunits (VH, VW) LiesintheirreFerencepoint: PXISFixedandbasedonpixelvalues, emissrelative EtothefontsizeFheelementoritsparent, Remisrelelatotherootfontsize, AndVH/VwarebaseDontheviewporttimensions.1.PXoffersprecis

Choosing the correct display value in CSS is crucial because it controls the behavior of elements in the layout. 1.inline: Make elements flow like text, without occupying a single line, and cannot directly set width and height, suitable for elements in text, such as; 2.block: Make elements exclusively occupy one line and occupy all width, can set width and height and inner and outer margins, suitable for structured elements, such as; 3.inline-block: has both block characteristics and inline layout, can set size but still display in the same line, suitable for horizontal layouts that require consistent spacing; 4.flex: Modern layout mode, suitable for containers, easy to achieve alignment and distribution through justify-content, align-items and other attributes, yes

CSSHoudini is a set of APIs that allow developers to directly manipulate and extend the browser's style processing flow through JavaScript. 1. PaintWorklet controls element drawing; 2. LayoutWorklet custom layout logic; 3. AnimationWorklet implements high-performance animation; 4. Parser&TypedOM efficiently operates CSS properties; 5. Properties&ValuesAPI registers custom properties; 6. FontMetricsAPI obtains font information. It allows developers to expand CSS in unprecedented ways, achieve effects such as wave backgrounds, and have good performance and flexibility

CSSgradientsenhancebackgroundswithdepthandvisualappeal.1.Startwithlineargradientsforsmoothcolortransitionsalongaline,specifyingdirectionandcolorstops.2.Useradialgradientsforcirculareffects,adjustingshapeandcenterposition.3.Layermultiplegradientstocre

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

The key to maintaining CSS for large applications is organizational structure, naming specifications, and tool assistance. First, adopt component management, split styles and implement local scopes to avoid conflicts; second, unify naming specifications such as BEM, SMACSS or namespace prefixes to improve maintainability; third, use PostCSS, stylelint and other tools to achieve automated processing and code quality control. Although these methods are not complicated, they require teamwork and continuous maintenance to be effectively implemented.

In Vue, provide and inject are features for directly passing data across hierarchical components. The parent component provides data or methods through provide, and descendant components directly inject and use these data or methods through inject, without passing props layer by layer; 2. It is suitable for avoiding "propdrilling", such as passing global or shared data such as topics, user status, API services, etc.; 3. Note when using: non-responsive original values ??must be wrapped into responsive objects to achieve responsive updates, and should not be abused to avoid affecting maintainability.
