You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/essentials/conditional.md
+25-23Lines changed: 25 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -8,41 +8,29 @@ The directive `v-if` is used to conditionally render a block. The block will onl
8
8
<h1 v-if="awesome">Vue is awesome!</h1>
9
9
```
10
10
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`:
12
14
13
15
```vue-html
14
16
<h1 v-if="awesome">Vue is awesome!</h1>
15
17
<h1 v-else>Oh no 😢</h1>
16
18
```
17
19
18
-
### Conditional Groups with `v-if` on `<template>`
20
+
<divclass="composition-api">
19
21
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)
21
23
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
+
<divclass="options-api">
29
26
30
-
### `v-else`
27
+
[Try it in the Playground](https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdD5cbmV4cG9ydCBkZWZhdWx0IHtcbiAgZGF0YSgpIHtcbiAgXHRyZXR1cm4ge1xuXHQgICAgYXdlc29tZTogdHJ1ZVxuICBcdH1cblx0fVxufVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cblx0PGgxIHYtaWY9XCJhd2Vzb21lXCI+VnVlIGlzIGF3ZXNvbWUhPC9oMT5cblx0PGgxIHYtZWxzZT5PaCBubyDwn5iiPC9oMT5cbjwvdGVtcGxhdGU+IiwiaW1wb3J0LW1hcC5qc29uIjoie1xuICBcImltcG9ydHNcIjoge1xuICAgIFwidnVlXCI6IFwiaHR0cHM6Ly9zZmMudnVlanMub3JnL3Z1ZS5ydW50aW1lLmVzbS1icm93c2VyLmpzXCJcbiAgfVxufSJ9)
31
28
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
37
29
</div>
38
-
<div v-else>
39
-
Now you don't
40
-
</div>
41
-
```
42
30
43
31
A `v-else` element must immediately follow a `v-if` or a `v-else-if` element - otherwise it will not be recognized.
44
32
45
-
###`v-else-if`
33
+
## `v-else-if`
46
34
47
35
The `v-else-if`, as the name suggests, serves as an "else if block" for `v-if`. It can also be chained multiple times:
48
36
@@ -63,6 +51,20 @@ The `v-else-if`, as the name suggests, serves as an "else if block" for `v-if`.
63
51
64
52
Similar to `v-else`, a `v-else-if` element must immediately follow a `v-if` or a `v-else-if` element.
65
53
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
+
66
68
## `v-show`
67
69
68
70
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
87
89
88
90
## `v-if` with `v-for`
89
91
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.
92
94
:::
93
95
94
96
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