Skip to content

Commit a879460

Browse files
committed
v-if and v-for
1 parent dc98d29 commit a879460

File tree

2 files changed

+255
-231
lines changed

2 files changed

+255
-231
lines changed

src/guide/essentials/conditional.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,29 @@ The directive `v-if` is used to conditionally render a block. The block will onl
88
<h1 v-if="awesome">Vue is awesome!</h1>
99
```
1010

11-
It is also possible to add an "else block" with `v-else`:
11+
## `v-else`
12+
13+
You can use the `v-else` directive to indicate an "else block" for `v-if`:
1214

1315
```vue-html
1416
<h1 v-if="awesome">Vue is awesome!</h1>
1517
<h1 v-else>Oh no 😢</h1>
1618
```
1719

18-
### Conditional Groups with `v-if` on `<template>`
20+
<div class="composition-api">
1921

20-
Because `v-if` is a directive, it has to be attached to a single element. But what if we want to toggle more than one element? In this case we can use `v-if` on a `<template>` element, which serves as an invisible wrapper. The final rendered result will not include the `<template>` element.
22+
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgYXdlc29tZSA9IHJlZih0cnVlKVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cblx0PGgxIHYtaWY9XCJhd2Vzb21lXCI+VnVlIGlzIGF3ZXNvbWUhPC9oMT5cblx0PGgxIHYtZWxzZT5PaCBubyDwn5iiPC9oMT5cbjwvdGVtcGxhdGU+IiwiaW1wb3J0LW1hcC5qc29uIjoie1xuICBcImltcG9ydHNcIjoge1xuICAgIFwidnVlXCI6IFwiaHR0cHM6Ly9zZmMudnVlanMub3JnL3Z1ZS5ydW50aW1lLmVzbS1icm93c2VyLmpzXCJcbiAgfVxufSJ9)
2123

22-
```vue-html
23-
<template v-if="ok">
24-
<h1>Title</h1>
25-
<p>Paragraph 1</p>
26-
<p>Paragraph 2</p>
27-
</template>
28-
```
24+
</div>
25+
<div class="options-api">
2926

30-
### `v-else`
27+
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdD5cbmV4cG9ydCBkZWZhdWx0IHtcbiAgZGF0YSgpIHtcbiAgXHRyZXR1cm4ge1xuXHQgICAgYXdlc29tZTogdHJ1ZVxuICBcdH1cblx0fVxufVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cblx0PGgxIHYtaWY9XCJhd2Vzb21lXCI+VnVlIGlzIGF3ZXNvbWUhPC9oMT5cblx0PGgxIHYtZWxzZT5PaCBubyDwn5iiPC9oMT5cbjwvdGVtcGxhdGU+IiwiaW1wb3J0LW1hcC5qc29uIjoie1xuICBcImltcG9ydHNcIjoge1xuICAgIFwidnVlXCI6IFwiaHR0cHM6Ly9zZmMudnVlanMub3JnL3Z1ZS5ydW50aW1lLmVzbS1icm93c2VyLmpzXCJcbiAgfVxufSJ9)
3128

32-
You can use the `v-else` directive to indicate an "else block" for `v-if`:
33-
34-
```vue-html
35-
<div v-if="Math.random() > 0.5">
36-
Now you see me
3729
</div>
38-
<div v-else>
39-
Now you don't
40-
</div>
41-
```
4230

4331
A `v-else` element must immediately follow a `v-if` or a `v-else-if` element - otherwise it will not be recognized.
4432

45-
### `v-else-if`
33+
## `v-else-if`
4634

4735
The `v-else-if`, as the name suggests, serves as an "else if block" for `v-if`. It can also be chained multiple times:
4836

@@ -63,6 +51,20 @@ The `v-else-if`, as the name suggests, serves as an "else if block" for `v-if`.
6351

6452
Similar to `v-else`, a `v-else-if` element must immediately follow a `v-if` or a `v-else-if` element.
6553

54+
## `v-if` on `<template>`
55+
56+
Because `v-if` is a directive, it has to be attached to a single element. But what if we want to toggle more than one element? In this case we can use `v-if` on a `<template>` element, which serves as an invisible wrapper. The final rendered result will not include the `<template>` element.
57+
58+
```vue-html
59+
<template v-if="ok">
60+
<h1>Title</h1>
61+
<p>Paragraph 1</p>
62+
<p>Paragraph 2</p>
63+
</template>
64+
```
65+
66+
`v-else` and `v-else-if` can also be used on `<template>`.
67+
6668
## `v-show`
6769

6870
Another option for conditionally displaying an element is the `v-show` directive. The usage is largely the same:
@@ -87,8 +89,8 @@ Generally speaking, `v-if` has higher toggle costs while `v-show` has higher ini
8789

8890
## `v-if` with `v-for`
8991

90-
::: tip Note
91-
Using `v-if` and `v-for` together is **not recommended**. See the [style guide](/style-guide/#avoid-v-if-with-v-for-essential) for further information.
92+
::: warning Note
93+
It's **not** recommended to use `v-if` and `v-for` on the same element due to implicit precedence. Refer to [style guide](/style-guide/#avoid-v-if-with-v-for-essential) for details.
9294
:::
9395

9496
When `v-if` and `v-for` are both used on the same element, `v-if` will be evaluated first. See the [list rendering guide](list#v-for-with-v-if) for details.

0 commit comments

Comments
 (0)