search
HomeWeb Front-endJS TutorialThe problem and solution of {{}} flickering when vue renders

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
Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

See all articles

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.