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/api/directives.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,7 @@
62
62
63
63
This directive triggers transitions when its condition changes.
64
64
65
-
When used together with `v-if`, `v-for`has a higher priority than v-if. See the [list rendering guide](../guide/list.html#v-for-with-v-if) for details.
65
+
When used together, `v-if`has a higher priority than `v-for`. We don't recommend using these two directives together on one element — see the [list rendering guide](../guide/list.html#v-for-with-v-if) for details.
Copy file name to clipboardExpand all lines: src/guide/conditional.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -91,4 +91,4 @@ Generally speaking, `v-if` has higher toggle costs while `v-show` has higher ini
91
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
92
:::
93
93
94
-
When used together with `v-if`, `v-for`has a higher priority than `v-if`. See the [list rendering guide](list#v-for-with-v-if) for details.
94
+
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.
Copy file name to clipboardExpand all lines: src/guide/list.md
+7-8Lines changed: 7 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -268,25 +268,24 @@ Similar to template `v-if`, you can also use a `<template>` tag with `v-for` to
268
268
Note that it's **not** recommended to use `v-if` and `v-for` together. Refer to [style guide](../style-guide/#avoid-v-if-with-v-for-essential) for details.
269
269
:::
270
270
271
-
When they exist on the same node, `v-for` has a higher priority than `v-if`. That means the `v-if` will be run on each iteration of the loop separately. This can be useful when you want to render nodes for only _some_ items, like below:
271
+
When they exist on the same node, `v-if` has a higher priority than `v-for`. That means the `v-if`condition will not have access to variables from the scope of the `v-for`:
272
272
273
273
```html
274
+
<!-- This will throw an error because property "todo" is not defined on instance. -->
275
+
274
276
<liv-for="todo in todos"v-if="!todo.isComplete">
275
277
{{ todo }}
276
278
</li>
277
279
```
278
280
279
-
The above only renders the todos that are not complete.
280
-
281
-
If instead, your intent is to conditionally skip execution of the loop, you can place the `v-if` on a wrapper element (or [`<template>`](conditional#conditional-groups-with-v-if-on-lt-template-gt)). For example:
281
+
This can be fixed by moving `v-for` to a wrapping `<template>` tag:
Copy file name to clipboardExpand all lines: src/style-guide/README.md
+17-62Lines changed: 17 additions & 62 deletions
Original file line number
Diff line number
Diff line change
@@ -196,7 +196,7 @@ There are two common cases where this can be tempting:
196
196
- To avoid rendering a list if it should be hidden (e.g. `v-for="user in users" v-if="shouldShowUsers"`). In these cases, move the `v-if` to a container element (e.g. `ul`, `ol`).
197
197
198
198
::: details Detailed Explanation
199
-
When Vue processes directives, `v-for` has a higher priority than `v-if`, so that this template:
199
+
When Vue processes directives, `v-if` has a higher priority than `v-for`, so that this template:
200
200
201
201
```html
202
202
<ul>
@@ -210,19 +210,9 @@ When Vue processes directives, `v-for` has a higher priority than `v-if`, so tha
210
210
</ul>
211
211
```
212
212
213
-
Will be evaluated similar to:
213
+
Will throw an error, because the `v-if` directive will be evaluated first and the iteration variable `user` does not exist at this moment.
214
214
215
-
```js
216
-
this.users.map(user=> {
217
-
if (user.isActive) {
218
-
returnuser.name
219
-
}
220
-
})
221
-
```
222
-
223
-
So even if we only render elements for a small fraction of users, we have to iterate over the entire list every time we re-render, whether or not the set of active users has changed.
224
-
225
-
By iterating over a computed property instead, like this:
215
+
This could be fixed by iterating over a computed property instead, like this:
226
216
227
217
```js
228
218
computed: {
@@ -243,40 +233,18 @@ computed: {
243
233
</ul>
244
234
```
245
235
246
-
We get the following benefits:
236
+
Alternatively, we can use a `<template>` tag with `v-for` to wrap the `<li>` element:
247
237
248
-
- The filtered list will _only_ be re-evaluated if there are relevant changes to the `users` array, making filtering much more efficient.
249
-
- Using `v-for="user in activeUsers"`, we _only_ iterate over active users during render, making rendering much more efficient.
250
-
- Logic is now decoupled from the presentation layer, making maintenance (change/extension of logic) much easier.
251
-
252
-
We get similar benefits from updating:
253
-
254
-
```html
238
+
```html
255
239
<ul>
256
-
<li
257
-
v-for="user in users"
258
-
v-if="shouldShowUsers"
259
-
:key="user.id"
260
-
>
261
-
{{ user.name }}
262
-
</li>
240
+
<templatev-for="user in users":key="user.id">
241
+
<liv-if="user.isActive">
242
+
{{ user.name }}
243
+
</li>
244
+
</template>
263
245
</ul>
264
246
```
265
247
266
-
to:
267
-
268
-
```html
269
-
<ulv-if="shouldShowUsers">
270
-
<li
271
-
v-for="user in users"
272
-
:key="user.id"
273
-
>
274
-
{{ user.name }}
275
-
</li>
276
-
</ul>
277
-
```
278
-
279
-
By moving the `v-if` to a container element, we're no longer checking `shouldShowUsers` for _every_ user in the list. Instead, we check it once and don't even evaluate the `v-for` if `shouldShowUsers` is false.
280
248
:::
281
249
282
250
<divclass="style-example style-example-bad">
@@ -293,18 +261,6 @@ By moving the `v-if` to a container element, we're no longer checking `shouldSho
293
261
</li>
294
262
</ul>
295
263
```
296
-
297
-
```html
298
-
<ul>
299
-
<li
300
-
v-for="user in users"
301
-
v-if="shouldShowUsers"
302
-
:key="user.id"
303
-
>
304
-
{{ user.name }}
305
-
</li>
306
-
</ul>
307
-
```
308
264
</div>
309
265
310
266
<divclass="style-example style-example-good">
@@ -321,14 +277,13 @@ By moving the `v-if` to a container element, we're no longer checking `shouldSho
0 commit comments