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)

React itself does not directly manage focus or accessibility, but provides tools to effectively deal with these issues. 1. Use Refs to programmatically manage focus, such as setting element focus through useRef; 2. Use ARIA attributes to improve accessibility, such as defining the structure and state of tab components; 3. Pay attention to keyboard navigation to ensure that the focus logic in components such as modal boxes is clear; 4. Try to use native HTML elements to reduce the workload and error risk of custom implementation; 5. React assists accessibility by controlling the DOM and adding ARIA attributes, but the correct use still depends on developers.

Shallowrenderingtestsacomponentinisolation,withoutchildren,whilefullrenderingincludesallchildcomponents.Shallowrenderingisgoodfortestingacomponent’sownlogicandmarkup,offeringfasterexecutionandisolationfromchildbehavior,butlacksfulllifecycleandDOMinte

StrictMode does not render any visual content in React, but it is very useful during development. Its main function is to help developers identify potential problems, especially those that may cause bugs or unexpected behavior in complex applications. Specifically, it flags unsafe lifecycle methods, recognizes side effects in render functions, and warns about the use of old string refAPI. In addition, it can expose these side effects by intentionally repeating calls to certain functions, thereby prompting developers to move related operations to appropriate locations, such as the useEffect hook. At the same time, it encourages the use of newer ref methods such as useRef or callback ref instead of string ref. To use Stri effectively

Create TypeScript-enabled projects using VueCLI or Vite, which can be quickly initialized through interactive selection features or using templates. Use tags in components to implement type inference with defineComponent, and it is recommended to explicitly declare props and emits types, and use interface or type to define complex structures. It is recommended to explicitly label types when using ref and reactive in setup functions to improve code maintainability and collaboration efficiency.

Server-siderendering(SSR)inNext.jsgeneratesHTMLontheserverforeachrequest,improvingperformanceandSEO.1.SSRisidealfordynamiccontentthatchangesfrequently,suchasuserdashboards.2.ItusesgetServerSidePropstofetchdataperrequestandpassittothecomponent.3.UseSS

WebAssembly(WASM)isagame-changerforfront-enddevelopersseekinghigh-performancewebapplications.1.WASMisabinaryinstructionformatthatrunsatnear-nativespeed,enablinglanguageslikeRust,C ,andGotoexecuteinthebrowser.2.ItcomplementsJavaScriptratherthanreplac

Vite or VueCLI depends on project requirements and development priorities. 1. Startup speed: Vite uses the browser's native ES module loading mechanism, which is extremely fast and cold-start, usually completed within 300ms, while VueCLI uses Webpack to rely on packaging and is slow to start; 2. Configuration complexity: Vite starts with zero configuration, has a rich plug-in ecosystem, which is suitable for modern front-end technology stacks, VueCLI provides comprehensive configuration options, suitable for enterprise-level customization but has high learning costs; 3. Applicable project types: Vite is suitable for small projects, rapid prototype development and projects using Vue3, VueCLI is more suitable for medium and large enterprise projects or projects that need to be compatible with Vue2; 4. Plug-in ecosystem: VueCLI is perfect but has slow updates,

Immutable updates are crucial in React because it ensures that state changes can be detected correctly, triggering component re-rendering and avoiding side effects. Directly modifying state, such as push or assignment, will cause React to be unable to detect changes. The correct way to do this is to create new objects instead of old objects, such as updating an array or object using the expand operator. For nested structures, you need to copy layer by layer and modify only the target part, such as using multiple expansion operators to deal with deep attributes. Common operations include updating array elements with maps, deleting elements with filters, adding elements with slices or expansion. Tool libraries such as Immer can simplify the process, allowing "seemingly" to modify the original state but generate new copies, but increase project complexity. Key tips include each
