惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
C
Cisco Blogs
The Hacker News
The Hacker News
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
Vercel News
Vercel News
C
CERT Recently Published Vulnerability Notes
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
I
Intezer
aimingoo的专栏
aimingoo的专栏
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
P
Proofpoint News Feed
B
Blog
T
Threat Research - Cisco Blogs
博客园 - 叶小钗
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Schneier on Security
Schneier on Security
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
H
Help Net Security
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
GRAHAM CLULEY

博客园 - zhouyu

shardingsphere实现按月分表 Vue3手册译稿 - 深入组件 - 插槽 vscode Vue3 多根节点语法检验错误fix Vue3手册译稿 - 深入组件 - 自定义事件 Vue3手册译稿 - 深入组件 - 非prop属性 Vue3手册译稿 - 深入组件 - pros Vue3手册译稿 - 深入组件 - 组件注册 Vue3手册译稿 - 基础 - 组件基础 Vue3手册译稿 - 基础 - 表单输入绑定 Vue3手册译稿 - 基础 - 事件处理 Vue3手册译稿 - 基础 - 列表渲染 Vue3手册译稿 - 基础 - Class和Style绑定 Vue3手册译稿 - 基础 - 计算属性及监听器 Vue3手册译稿 - 基础 - Data属性及方法 Vue3手册译稿 - 基础 - 模板语法 Vue3手册译稿 - 基础 - 应用&组件实例 Vue3手册译稿 - 基础 - 介绍 Vue3手册译稿 - 基础 - 安装 元旦三天假期,实现一个电商退单管理系统【四】-手机客户端实现
Vue3手册译稿 - 基础 - 条件渲染
zhouyu · 2021-03-03 · via 博客园 - zhouyu

条件渲染

v-if

v-if用来根据条件渲染块元素。只有v-if结果为真时才会显示该块元素。
<span v-if="awesome">Vue is awesome</span>
同时也可以添加v-else块:

<h1 v-if="awesome">Vue is awesome</h1>
<h1 v-else> Oh no :( </h1>

template中使用v-if进行条件分组

因为v-if是一个指令,所以只能应用在一个标签上。在<template>标签上使用v-if可以控制多个标签,就像提供一个隐形包。最终的渲染结果是不包含<template的:

<template v-if="ok">
  <h1>Title</h1>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
</template>

以上代码放到一个完整例子里:

<html lang="en-US">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <title>vue demo1</title>
      <script src="https://unpkg.com/vue@next"></script>
     </head>
     <body>
        <div id="app">
            <div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>
            <span v-if="awesome">Vue is awesome</span>
            <h1 v-if="awesome">Vue is awesome</h1>
            <h1 v-else> Oh no :( </h1>
            <template v-if="awesome">
                <h1>标题</h1>
                <p>段落 1</p>
                <p>段落 2</p>
            </template>
        </div>
        <script type="text/javascript">
            Vue.createApp({
                data() {
                    return {
                        awesome: true
                    }
                }
            }).mount('#app')
        </script>
    </body>
</html>

v-else

你可以使用v-elsev-if添加一个else块:

<div v-if="Math.random() > 0.5">
  随机数大于0.5你就会看的见我!
</div>
<div v-else>
    随机数小于等于0.5你就会看见我!
</div>

v-else必须紧跟v-ifv-else-if,否则将识别不了。

v-else-if
字如其义,为v-if提供一个else if块,且可以连续使用多个:

<div v-if="type === 'A'">
  A
</div>
<div v-else-if="type === 'B'">
  B
</div>
<div v-else-if="type === 'C'">
  C
</div>
<div v-else>
  Not A/B/C
</div>

v-else一样,v-else-if必须紧跟v-ifv-else-if标签。

v-show

v-show是另一个控制标签显示的指令。使用方法基本上是一样的:

<h1 v-show="ok">你好吗?</h1>

不同点是:v-show的标签是一直存在DOM中,只是添加display属性控制显示或隐藏。
v-show不支持应用在<template>标签上,也不支持v-else一起使用。
v-if是“真正”的条件渲染,它会销毁和重新创建DOM元素及事件监听。
v-if也是懒加载的:如果初始化渲染时条件是假,则不会做任何事情 -- 除非当条件首次更新为真否则条件块(即应用的标签)是不会被渲染的。
相比而言,v-show简单的多 - 无论如何都会渲染标签,通过CSS(display)控制显示或隐藏罢了。
通俗的来说,v-if控制更新时消耗资源多,v-show首次渲染时消耗资源多。所以v-show适用于经常更新状态,v-if适用条件不会在运行时更新场景。

v-ifv-for

[warning] 不推荐v-ifv-for同时使用。参考样式手册获取更多信息。

v-ifv-for同时应用在一个标签上时,v-if会优先运算。参考列表渲染手册查看详情。