Home > Web Front-end > JS Tutorial > body text

How to deal with flickering when rendering in Vue

php中世界最好的语言
Release: 2018-05-08 11:52:24
Original
2129 people have browsed it

This time I will show you how to deal with flickering when vue renders. What are the precautions for dealing with flickering when vue renders. The following is a practical case, let's take a look.

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-if may also include data binding 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 suitable to use v-if and when to use v-show will also change 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 multiple conditional judgments, but there are only v-if and v in vue -else, there is no such instruction as v-elseif. Although there is no similar instruction, there are still several ways to solve this problem.

Method 1: template

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

Method 2: partial

The element is the registered partial slot, partial Compiled by Vue on insertion. The elements themselves are replaced. The element needs to specify the name attribute.

This thing is reminiscent of the javaScript native fragmentation 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('my-partial', '<p>This is a partial! {{msg}}</p>')
<!-- 静态 partial -->
<partial name="my-partial"></partial>
<!-- 动态 partial -->
<!-- 渲染 partial,id === vm.partialId -->
<partial v-bind:name="partialId"></partial>
Copy after login

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 Multiple conditional 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 because of the priority v-else of the command. The solution is to replace the v-else

official one with another v-show. An 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>
Copy after login

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

When writing the APP community page I used v-show in some places. When I refreshed the page, I found that even when the logic was judged to be false, some elements would still show their faces and should not be displayed. It flashed by and the elements were small, which is fine. If it is special It looked very unpleasant in a big place, so I searched online to see if there was any 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 it was followed by the usage:

和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Mustache 标签直到实例准备完毕。

也就是说v-cloak指令可以像CSS选择器一样绑定一套CSS样式然后这套CSS会一直生效到实例编译结束。

 示例代码:

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

 方法二: v-text

vue中我们会将数据包在两个大括号中,然后放到HTML里,但是在vue内部,所有的双括号会被编译成textNode的一个v-text指令。

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

示例代码:

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

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Vue.js实现可配置登录表单步骤详解

怎样传递数组参数至后台

The above is the detailed content of How to deal with flickering when rendering in Vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template