Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of v-if and v-show in vuejs

Detailed explanation of the use of v-if and v-show in vuejs

php中世界最好的语言
php中世界最好的语言Original
2018-05-08 15:47:494477browse

This time I will bring you a detailed explanation of the use of v-if and v-show in vuejs. What are the precautions for using v-if and v-show in vuejs. The following is a practical case. Let’s take a look. take a look.

1. Official website concept description

v-if is a 'real' conditional rendering, because it will ensure that

events within the conditional block during the switching process Listeners and child components are destroyed and recreated appropriately.

v-if is also lazy, if the condition is false on initial render, then does nothing - until the condition is true for the first time The conditional block will only start rendering when the conditional block is started. In comparison, v-show is much simpler - no matter what the initial condition is, the element will always be rendered, and it is simply switched based on css.

Generally speaking, v-if has a higher switching overhead, while v-show has a higher rendering overhead. Therefore, if very frequent switching is required, it is better to use v-show; if the conditions at runtime are not good It may change, it is better to use v-if.

2.Practical results

Excerpt: If v-if is used, the entire dom The structure will not appear on the page at all. If you use v-show, it depends on the following conditions. If it is true, it will be displayed. If it is false, add

style="display <a href="http://www.php.cn/wiki/927.html" target="_blank">:none</a>”. Therefore, if it is a large component such as a component, I personally think it is better to use v-if. If it is something that is temporarily hidden and will be displayed later, v-show is more convenient. . Comparing v-style and v-show, v-show is equivalent to the shortcut of v-style="display:none" and v-style="display:block" .

1. v-show does not work problem

Recently I am using

vue_element-ui to develop multi-page applications. Among them, I encountered the problem that v-show does not work.

a. The problem description is as shown below (the expected effect), where TableThe data changes dynamically, including the title, which will also change according to the background data. If the title returned by the background is empty, the content of the column will not be displayed. Otherwise, all data of the column will be displayed.

Part of the code is as follows:

The effect diagram that appears when executing the above picture is as follows:

Then the effect as shown above will appear, that is, v-show failed to hide the column data with the title value being null

b. Solution:

Change v-show to v-if to achieve the effect in Figure 1.

c. Summary (personal opinion):

Since el-table-column will generate multi-line label elements, and v-show does not support template syntax, it can be inferred that v-show cannot display or hide multiple elements? I wonder if

It can be understood this way, I hope God will tell you! So in this case, it can only be achieved with v-if.

In addition, when rendering multiple elements, you can put a

< template> element is used as a packaging element, and v-if is used on it to perform conditional judgment. The final rendering will not include this element. At the same time, v-show is not supported