Home Web Front-end Front-end Q&A What are the differences between v-if and v-show in vue

What are the differences between v-if and v-show in vue

Nov 01, 2021 am 11:35 AM
v-if v-show vue

Difference: 1. "v-if" dynamically adds and deletes DOM elements based on judgment conditions, "v-show" dynamically displays and hides elements based on judgment conditions; 2. "v- The switching cost of "if" is high, and the initial rendering cost of "v-show" is high; 3. The performance of "v-show" is higher than that of "v-if", etc.

What are the differences between v-if and v-show in vue

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

The difference between v-if and v-show is a basic knowledge point often asked in front-end interviews. v-if and v-show, as their names suggest, are used to judge the display effect of the view layer. So how is it displayed specifically? What is the difference between v-if and v-show?

First of all, we can take a look at the introduction of the Vue Chinese community documentation:

The Vue Chinese community documentation simply says: during initial rendering Perform conditional judgment display;

In actual development, we often use v-if and v-show for judgment display. We can understand it this way:

<div class="tc" v-if="pload && list.length<1" >
   <img src="../assets/img/mall_none_order.png" />
</div>
Copy after login
Copy after login

In the above example, v The function of -if is to determine whether the div is displayed based on whether upload and list.length are <1. Because there is a relationship, only when both upload and list.length <1 are true, the div and the img inside will be displayed. Display and take effect,

On the contrary, if the judgment condition is not established, v-if will not be displayed;

<div class="tc" v-if="pload && list.length<1" >
   <img src="../assets/img/mall_none_order.png" />
</div>
Copy after login
Copy after login

We can actually change the above case code to v-show for judgment. The visual effect is also the same; note: only the visual effect is the same, there is no equal relationship between the two (analyzed later); use v-show to judge that the condition load is true and list.length

As mentioned above, v-if and v-show can achieve the effect of displaying based on judgment conditions, but this does not mean that the two are equal; It’s just that the visual effects are the same. Let’s talk about the differences and precautions between v-if and v-show:

 

Using v-if, we can notice that when browsing The browser did not render the div with the element class tc. This is because v-if only when the judgment condition is true, the browser will generate the tag and render it during browsing. Otherwise, when the judgment condition is false, the browser will not Generated tags will not be rendered.

Then we can think about it. When the condition is false, the browser does not generate tags and does not render. When the condition is true, the browser only renders. Does this consume a lot of page performance?

What if our judgment conditions are not just one scenario but multiple scenarios? So what should we do now? Vue provides us with the v-else instruction. v-else is an exclusive instruction for v-if. v-else can only be used together with v-if;

 

If we Using v-show for judgment rendering, the div tag and the img inside will be generated and rendered. However, we noticed that the browser added the display:none attribute to our inline style, so in essence the v-show tag is It exists, but the browser hides it for us;

In fact, the browser only changes the inline style based on the judgment condition. When the condition is true, the inline style display:block; when the condition is false, the inline style is displayed: block. Change the joint style to display:none;

v-show is actually more performant than v-if, because v-show only dynamically changes the style without adding or deleting DOM elements, but it encounters many situations v-show cannot be used in conjunction with v-else when making branch judgments. The solution to using v-show in such a scenario is to use v-show again to write another logical judgment;

Summary of differences:

1. The principle of v-if is to dynamically add and delete DOM elements based on judgment conditions, while v-show is to dynamically display based on judgment conditions. and hidden elements. Frequent addition and deletion of DOM operations will affect page loading speed and performance. From this we can draw the conclusion:

When your project program is not very large, v-if and v-show can be used to judge display and hide (using v-if in this scenario has little impact, not no impact);

It is not recommended to use it when your project program is relatively large. v-if is used to judge display and hide. It is recommended to use v-show;

2. v-if switching has a partial compilation/uninstallation process. During the switching process, the internal event listeners and subsystems are properly destroyed and rebuilt. Component; v-show is just a simple switch based on css;

3. v-if is lazy. If the initial condition is false, it will do nothing; only when the condition becomes true for the first time Start partial compilation (is the compilation cached? After the compilation is cached, partial uninstallation is performed when switching); v-show is compiled under any conditions (whether the first condition is true), and then cached, and the DOM elements are retained ;

4. v-if has a higher switching cost; v-show has a higher initial rendering cost;

If frequent switching is required, it is better to use v-show. If the operating conditions rarely change, it is better to use v-if.

5. v-show has higher performance than v-if

Because v-show can only dynamically change styles without adding or deleting DOM elements. Therefore, when the program is not very large, there is not much difference between v-if and v-show. If the project is very large, it is recommended to use v-show more, which will reduce the performance of the browser's later operations.

6. v-if is suitable for operating conditions that are unlikely to change; v-show is suitable for frequent switching.

Related recommendations: "vue.js Tutorial"

The above is the detailed content of What are the differences between v-if and v-show in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does it mean to lazy load vue? What does it mean to lazy load vue? Apr 07, 2025 pm 11:54 PM

In Vue.js, lazy loading allows components or resources to be loaded dynamically as needed, reducing initial page loading time and improving performance. The specific implementation method includes using &lt;keep-alive&gt; and &lt;component is&gt; components. It should be noted that lazy loading can cause FOUC (splash screen) issues and should be used only for components that need lazy loading to avoid unnecessary performance overhead.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to query the version of vue How to query the version of vue Apr 07, 2025 pm 11:24 PM

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the &lt;script&gt; tag in the HTML file that refers to the Vue file.

Vue realizes marquee/text scrolling effect Vue realizes marquee/text scrolling effect Apr 07, 2025 pm 10:51 PM

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with &lt;div&gt;. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

See all articles