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

Vue.js—Conditional rendering

不言
Release: 2018-03-31 13:57:00
Original
1451 people have browsed it

This article shares with you about Vue.js-conditional rendering. Friends in need can take a look at the content of this article


v- if


In string templates, such as Handlebars, we have to write a conditional block like this:

<!-- Handlebars 模板 -->{{#if ok}}
    <h1>Yes</h1>{{/if}}
Copy after login
Copy after login

In Vue , we use the v-if instruction to achieve the same function:

<h1 v-if="ok">Yes</h1>
Copy after login

You can also use v-else to add an "else block":

<h1 v-if="ok">Yes</h1>

No

Copy after login

# 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=&#39;ok&#39;>
        <h1>Title</h1>
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
    </template></p>
Copy after login
Copy after login
var vm = new Vue({
    el: &#39;#example&#39;,    data: {
        ok: true
    }
})
Copy after login
Copy after login

#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&#39;t
</p>
Copy after login
Copy after login
<p v-if="type === &#39;A&#39;">
    A
</p>
<p v-else-if="type === &#39;B&#39;">
    B
</p>
<p v-else-if="type === &#39;C&#39;">
    C
</p>
<p v-else>
    Not A/B/C
</p>
Copy after login
Copy after login

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>
Copy after login

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}}
Copy after login
Copy after login

In Vue, we use v The -if directive implements the same function:

<h1 v-if="ok">Yes</h1>
Copy after login

You can also use v-else to add an "else block":

<h1 v-if="ok">Yes</h1>

No

Copy after login

# 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=&#39;ok&#39;>
        <h1>Title</h1>
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
    </template></p>
Copy after login
Copy after login
var vm = new Vue({
    el: &#39;#example&#39;,    data: {
        ok: true
    }
})
Copy after login
Copy after login

#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&#39;t
</p>
Copy after login
Copy after login
<p v-if="type === &#39;A&#39;">
    A
</p>
<p v-else-if="type === &#39;B&#39;">
    B
</p>
<p v-else-if="type === &#39;C&#39;">
    C
</p>
<p v-else>
    Not A/B/C
</p>
Copy after login
Copy after login

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>
Copy after login

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:

vue.js+layer table data binding and update

Vue.js vue tag attributes and Conditional rendering

Detailed explanation of conditional rendering in mini program development

The above is the detailed content of Vue.js—Conditional rendering. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!