Detailed explanation of Vue.js conditional rendering
Mar 31, 2018 pm 05:00 PMThis article mainly shares with you the detailed explanation of Vue.js conditional rendering. In string templates, such as Handlebars, we have to write a conditional block like this:
<!-- Handlebars 模板 -->{{#if ok}} <h1>Yes</h1>{{/if}}
In In Vue, we use the v-if
directive to achieve the same function:
<h1 v-if="ok">Yes</h1>
You can also use v-else
to add an "else block":
<h1 v-if="ok">Yes</h1>No
# Use v-if
conditional rendering grouping on <template>
elements
becausev-if
is a directive, so it must be added to an element. But what if you want to switch multiple elements? At this time, you can treat a <template>
element as an invisible wrapping element, and use v-if
on it. The final rendering result will not contain <template>
elements.
<p id="example"> <template v-if='ok'> <h1>Title</h1> <p>Paragraph 1</p> <p>Paragraph 2</p> </template></p>
var vm = new Vue({ el: '#example', data: { ok: true } })
#v-else
and v-else-if
can be used v-else
directive to represent an "else block" of v-if
:
<p v-if="Math.random() > 0.5"> Now you see me</p> <p v-else> Now you don't </p>
<p v-if="type === 'A'"> A </p> <p v-else-if="type === 'B'"> B </p> <p v-else-if="type === 'C'"> C </p> <p v-else> Not A/B/C </p>
v-else
The element must immediately follow the v-if
or v-else-if
after the element, otherwise it will not be recognized.
Similar to v-else
, v-else-if
must also be followed immediately by v-if
or v- after the element of else-if
.
v-show
Another option for showing elements based on conditions is the v-show
directive.
<h1 v-show="ok">Hello!</h1>
The difference is that elements with v-show
will always be rendered and remain in the DOM. v-show
Simply toggle the element's CSS property display.
Note that v-show
does not support the <template>
element, nor does it support v-else
.
v-if vs v-show
v-if
is "real" conditional rendering, as it ensures that within the conditional block during the switch The event listeners and subcomponents are destroyed and recreated appropriately.
v-if
is also lazy: if the condition is false on initial rendering, nothing is done - rendering of the condition will not begin until the first time the condition becomes true piece.
In contrast, v-show
is much simpler - the element will always be rendered regardless of the initial conditions, and is simply toggled based on CSS.
Generally speaking, v-if
has higher switching overhead, while v-show
has higher initial rendering overhead. Therefore, if you need to switch very frequently, it is better to use v-show
; if the conditions rarely change at runtime, it is better to use v-if
.
v-if
In string templates, such as Handlebars, we have to write a conditional block like this:
<!-- Handlebars 模板 -->{{#if ok}} <h1>Yes</h1>{{/if}}
In Vue, we use v The -if
directive implements the same function:
<h1 v-if="ok">Yes</h1>
You can also use v-else
to add an "else block":
<h1 v-if="ok">Yes</h1>No
# in <template>
Use v-if
conditional rendering grouping on elements
Because v-if
is a directive, it must be It is added to an element. But what if you want to switch multiple elements? At this time, you can treat a <template>
element as an invisible wrapping element, and use v-if
on it. The final rendering result will not contain <template>
elements.
<p id="example"> <template v-if='ok'> <h1>Title</h1> <p>Paragraph 1</p> <p>Paragraph 2</p> </template></p>
var vm = new Vue({ el: '#example', data: { ok: true } })
#v-else
and v-else-if
can be used v-else
directive to represent an "else block" of v-if
:
<p v-if="Math.random() > 0.5"> Now you see me</p> <p v-else> Now you don't </p>
<p v-if="type === 'A'"> A </p> <p v-else-if="type === 'B'"> B </p> <p v-else-if="type === 'C'"> C </p> <p v-else> Not A/B/C </p>
v-else
The element must immediately follow the v-if
or v-else-if
after the element, otherwise it will not be recognized.
Similar to v-else
, v-else-if
must also be followed immediately by v-if
or v- after the element of else-if
.
v-show
Another option for showing elements based on conditions is the v-show
directive.
<h1 v-show="ok">Hello!</h1>
The difference is that elements with v-show
will always be rendered and remain in the DOM. v-show
Simply toggle the element's CSS property display.
Note that v-show
does not support the <template>
element, nor does it support v-else
.
v-if vs v-show
v-if
is "real" conditional rendering, as it ensures that within the conditional block during the switch The event listeners and subcomponents are destroyed and recreated appropriately.
v-if
is also lazy: if the condition is false on initial rendering, nothing is done - rendering of the condition will not begin until the first time the condition becomes true piece.
In contrast, v-show
is much simpler - the element will always be rendered regardless of the initial conditions, and is simply toggled based on CSS.
In general, v-if
has higher switching overhead, while v-show
has higher initial rendering overhead. Therefore, if you need to switch very frequently, it is better to use v-show
; if the conditions rarely change at runtime, it is better to use v-if
.
Related recommendations:
The above is the detailed content of Detailed explanation of Vue.js conditional rendering. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Vue.js is not difficult to learn, especially for developers with a JavaScript foundation. 1) Its progressive design and responsive system simplify the development process. 2) Component-based development makes code management more efficient. 3) The usage examples show basic and advanced usage. 4) Common errors can be debugged through VueDevtools. 5) Performance optimization and best practices, such as using v-if/v-show and key attributes, can improve application efficiency.

Vue.js is mainly used for front-end development. 1) It is a lightweight and flexible JavaScript framework focused on building user interfaces and single-page applications. 2) The core of Vue.js is its responsive data system, and the view is automatically updated when the data changes. 3) It supports component development, and the UI can be split into independent and reusable components.
