[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[Vue.js] Differences between v-if and v-show, points on how to use them properly

(In this article, operation was confirmed with Vue.js v2.5.13.)

I would like to write about the general differences and usage of Vue.js' conditional rendering directives v-if and v-show.

the official document v-if vs v-show and still don't get the hang of it will read this.

 

Difference between v-if and v-show

Try using it

I have created a JSFiddle to show you an example of usage, please take a look.

Also, please prepare Developer Tools. (If you are using Chrome, FireFox, or Edge, press F12 to open)

To briefly explain the sample, there are components A and B whose templates have only one p tag,

I am trying to switch display/hide using the v-if and v-show directives, respectively.

The data of the root object has two Boolean variables toggleA and toggleB that indicate display/hide, and
to embed component A, use the v-if directive (v-if="toggleA")
to embed component B. adds the v-show directive (v-show="toggleB").

For both toggleA and toggleB, when you click the corresponding button A or B, the truth value is reversed.

Now, when you open the page, I think only the A and B buttons for switching between true and false values ​​are displayed.
However, on the DOM tree, the difference between v-if and v-show is already visible when the page is opened, and
the DOM of component A does not exist, but the DOM of component B has the display:none style applied. It exists in a state where

This is what the official documentation says:

v-if is lazy drawing. If it is false in the initial display, nothing will be done. A conditional block is not drawn until the condition first becomes true.
On the other hand, v-show is very simple. Elements are always drawn regardless of initial conditions and saved as simple CSS-based toggles.

is what it means.

 

Let's go a little deeper

From here, we will see what happens when we repeatedly invert the truth values ​​of v-if and v-show.
Reload the page once (or press RUN on the JsFiddle menu) and take a look at the Developer Tools console.

You can see that the created hook of component B (v-show) is executed when you open the page.
Even if you press the B button repeatedly and the v-show changes repeatedly from false -> true -> false -> true, the created hook will only be executed once because the style only changes.

On the other hand, if you press the A button repeatedly, you will see that the created hook of the A component (v-if) will be executed many times.
When v-if switches from true to false, the DOM disappears, and when it switches to true and reappears, data has also been initialized.

 

Proper use

Use v-show if you frequently switch between display/hide or need to retain data even when hidden;
v- if you switch infrequently or do not need to retain data when hidden. It is a good idea to use if as your basic policy.

The v-else directive can only be used when using v-if, but it is not a good idea to use v-if just because you want to use v-else.

If you found this article helpful , please give it a like!
3
Loading...
3 votes, average: 1.00 / 13
10,261
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author