Home >Web Front-end >JS Tutorial >The problem and solution of {{}} flickering when vue renders

The problem and solution of {{}} flickering when vue renders

亚连
亚连Original
2018-05-28 15:57:532069browse

v-if and v-show may be the two most commonly used instructions in daily development. Although they seem to have similar functions, there are still big differences between them. Next, through this article, I will share with you the problem and solution of {{}} flickering during vue rendering. Friends who are interested should take a look.

v-if and v-show may be the most commonly used in daily development. Although the two instructions seem to have similar functions, there are still big differences between them.

The difference between v-if and v-show:

When switching v-if blocks, Vue.js has a partial compilation/uninstallation process, because templates within v-ifs may also include data bindings or subcomponents. v-if is true conditional rendering because it ensures that the event listeners and subcomponents within the conditional block are properly destroyed and rebuilt between switches.

v-if is also lazy: if the condition is false on the initial render, nothing is done - partial compilation starts only when the condition becomes true for the first time (compilation is cached).

In comparison, v-show is much simpler - elements are always compiled and retained, simply toggled based on CSS.

To put it simply, the biggest difference between the two is that v-if will only compile when the conditions are met, while v-show will always compile regardless of whether the conditions are met. The display and hiding of v-show is just simple. Switch the display attribute of CSS.

Applicable scenarios:

After understanding the essential difference between the two, when is it appropriate to use v-if and when is it appropriate to use v-show? Gotta be simple.

Generally speaking, v-if has higher switching cost and v-show has higher initial rendering cost. Therefore, v-show is better if you need to switch frequently, and v-if is better if conditions are unlikely to change at runtime.

For example, many pages now behave differently on different terminals. The most common one is that many APP pages will display download prompts when they are opened on WeChat, but not inside the APP, like this In this case, the status of each end is determined when loading and will not change, so it is suitable to use v-if, so that the downloaded part will not be compiled when it is opened in the APP.

It is most appropriate to use v-show in places where elements on the page are displayed/hidden according to different conditions, because basically the two states need to be switched frequently. As mentioned above, v- The switching cost of show is less than that of v-if.

Multiple conditions

Many times the code requires multi-condition judgment, but there are only v-if and v-else in vue, and there is no v- elseif directive. Although there is no similar instruction, there are still several ways to solve this problem.

Method one: template

<p v-if="xxx"></p>
<template v-else>
<p v-if="yyy"></p>
<p v-else></p>
</template>

Method two: partial

## The # element is the slot of a registered partial, which is compiled by Vue when inserted. The elements themselves are replaced. The element needs to specify the name attribute.

This thing is reminiscent of JavaScript's native fragment element, but it is not the same thing. Partials are divided into static and dynamic ones, and to solve the above problems, dynamic partials must be used.

Example:

// 注册 partial
Vue.partial(&#39;my-partial&#39;, &#39;<p>This is a partial! {{msg}}</p>&#39;)
<!-- 静态 partial -->
<partial name="my-partial"></partial>
<!-- 动态 partial -->
<!-- 渲染 partial,id === vm.partialId -->
<partial v-bind:name="partialId"></partial>

To solve the problem of multiple conditions, we can pre-register the respective partials for each situation, and then Bind the name attribute of the partial to the judgment condition, so that multiple condition judgments can be realized.

Others:

1. The v-if directive can be applied to the template packaging element, but v-show does not support template

2. When v-show is applied to a component, problems will occur due to the priority of the instruction v-else. The solution is to replace v-else with another v-show

The official example is as follows:

// 错误
<custom-component v-show="condition"></custom-component>
<p v-else>这可能也是一个组件</p>
// 正确做法
<custom-component v-show="condition"></custom-component>
<p v-show="!condition">这可能也是一个组件</p>


When the Vue page is loaded, the hidden element set by v-show appears, causing the page to flicker.

When I was writing the APP community page, I used v-show in some places. When refreshing the page, I found that even when the logic is judged to be false, some elements will show their faces when they should not be displayed. , it flashed by, and it was okay if the element was small, but if it was a particularly large place, it would look very unpleasant, so I searched online to see if there was a solution, and it turned out that there was indeed one.

Method 1: v-cloak

The official explanation is just one sentence: This instruction remains on the element until the associated instance ends compilation.

I was confused just by reading this sentence, but the usage was followed immediately:

When used together with CSS rules such as

[v-cloak] { display: none } , this directive hides uncompiled Mustache tags until the instance is ready.

That is to say, the v-cloak directive can bind a set of CSS styles like a CSS selector, and this set of CSS will take effect until the instance is compiled.

Sample code:

// <p> 不会显示,直到编译结束。
[v-cloak] {
display: none;
}
<p v-cloak>
{{ message }}
</p>

Method 2: v-text

We will use vue Wrap the data in two curly brackets and put it in HTML, but inside vue, all double brackets will be compiled into a v-text directive of textNode.


 而使用v-text的好处就是永远更好的性能,更重要的是可以避免FOUC (Flash of Uncompiled Content) ,也就是上面与遇到的问题。

示例代码:

<span v-text="msg"></span>
<!-- same as -->
<span>{{msg}}</span>

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

零基础学习AJAX之AJAX框架

零基础学习AJAX之制作自动校验的表单

ajax的get请求时缓存处理解决方法

The above is the detailed content of The problem and solution of {{}} flickering when vue renders. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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