Home Web Front-end JS Tutorial How to deal with flickering when rendering in Vue

How to deal with flickering when rendering in Vue

May 08, 2018 am 11:52 AM
deal with how flashing

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!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Summary of frequently asked questions about importing Excel data into Mysql: How to deal with error log problems encountered when importing data? Summary of frequently asked questions about importing Excel data into Mysql: How to deal with error log problems encountered when importing data? Sep 10, 2023 pm 02:21 PM

Summary of frequently asked questions about importing Excel data into Mysql: How to deal with error log problems encountered when importing data? Importing Excel data into a MySQL database is a common task. However, during this process, we often encounter various errors and problems. One of them is the error log issue. When we try to import data, the system may generate an error log listing the specific information about the error that occurred. So, how should we deal with the error log when we encounter this situation? First, we need to know how

A quick guide to CSV file manipulation A quick guide to CSV file manipulation Dec 26, 2023 pm 02:23 PM

Quickly learn how to open and process CSV format files. With the continuous development of data analysis and processing, CSV format has become one of the widely used file formats. A CSV file is a simple and easy-to-read text file with different data fields separated by commas. Whether in academic research, business analysis or data processing, we often encounter situations where we need to open and process CSV files. The following guide will show you how to quickly learn to open and process CSV format files. Step 1: Understand the CSV file format First,

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

A complete solution to the frequent flickering of Win10 computer screen A complete solution to the frequent flickering of Win10 computer screen Jan 16, 2024 pm 08:48 PM

After upgrading to the official version of Win10 and entering the Windows desktop, I found that the screen kept flickering and I could not perform tasks at this time. The editor recently encountered this problem after upgrading to the official version of Win10. After repeated thinking and exploration in the background, the problem was finally solved. Now I will share with you the specific solution. After upgrading to the official version of Win10, most users using the Win10 system have encountered the problem of screen flickering. This is usually due to running incompatible software or a graphics card driver failure. So what causes the Win10 screen to flicker? what can we do about it? The following are solutions to Win10 computer screen flickering. What should I do if my Win10 computer screen keeps flickering? Right-click "This" on the desktop.

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

How to solve the problem after the upgrade from win7 to win10 fails? How to solve the problem after the upgrade from win7 to win10 fails? Dec 26, 2023 pm 07:49 PM

If the operating system we use is win7, some friends may fail to upgrade from win7 to win10 when upgrading. The editor thinks we can try upgrading again to see if it can solve the problem. Let’s take a look at what the editor did for details~ What to do if win7 fails to upgrade to win10. Method 1: 1. It is recommended to download a driver first to evaluate whether your computer can be upgraded to Win10. 2. Then use the driver test after upgrading. Check if there are any driver abnormalities, and then fix them with one click. Method 2: 1. Delete all files under C:\Windows\SoftwareDistribution\Download. 2.win+R run "wuauclt.e

Exception handling and error logging skills in C# Exception handling and error logging skills in C# Oct 08, 2023 am 11:51 AM

Exception handling and error logging skills in C# Introduction: In the software development process, exception handling and error logging are very important links. For C# developers, mastering exception handling skills and error logging methods can help us better track and debug code, and improve the stability and maintainability of the program. This article will introduce commonly used exception handling techniques in C# and provide specific code examples to help readers better understand and apply exception handling and error logging. 1. Basic concepts of exception handling Exceptions refer to the

See all articles