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/v2/api/index.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1328,8 +1328,8 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
1328
1328
Conditionally render the element based on the truthy-ness of the expression value. The element and its contained directives / components are destroyed and re-constructed during toggles. If the element is a `<template>` element, its content will be extracted as the conditional block.
1329
1329
1330
1330
This directive triggers transitions when its condition changes.
1331
-
1332
-
<pclass="tip">When used together with v-if, v-for has a higher priority than v-if. Check the <ahref="../guide/list.html#V-for-and-v-if">list rendering guide</a> for details.</p>
1331
+
1332
+
<pclass="tip">When used together with v-if, v-for has a higher priority than v-if. See the <ahref="../guide/list.html#v-for-with-v-if">list rendering guide</a> for details.</p>
@@ -1413,9 +1413,9 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
1413
1413
{{ item.text }}
1414
1414
</div>
1415
1415
```
1416
-
1417
-
<pclass="tip">When used together with v-if, v-for has a higher priority than v-if. Check the <ahref="../guide/list.html#V-for-and-v-if">list rendering guide</a> for details.</p>
1418
-
1416
+
1417
+
<pclass="tip">When used together with v-if, v-for has a higher priority than v-if. See the <ahref="../guide/list.html#v-for-with-v-if">list rendering guide</a> for details.</p>
1418
+
1419
1419
The detailed usage for `v-for` is explained in the guide section linked below.
1420
1420
1421
1421
-**See also:**
@@ -1476,7 +1476,7 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
1476
1476
1477
1477
<!-- key modifier using keyCode -->
1478
1478
<input@keyup.13="onEnter">
1479
-
1479
+
1480
1480
<!-- the click event will be triggered at most once -->
1481
1481
<buttonv-on:click.once="doThis"></button>
1482
1482
```
@@ -1527,7 +1527,7 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
Copy file name to clipboardExpand all lines: src/v2/guide/list.md
+14-74Lines changed: 14 additions & 74 deletions
Original file line number
Diff line number
Diff line change
@@ -329,88 +329,28 @@ new Vue({
329
329
</script>
330
330
{% endraw %}
331
331
332
-
### `v-for`and`v-if`
332
+
### `v-for`with`v-if`
333
333
334
-
In Vue template, `v-for` has a higher priority than `v-if` when they exists in the same node.
334
+
When they exists 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 is very useful when you want to render nodes for only _some_ items, like below:
335
335
336
-
Suppose the example code bellow:
337
-
338
-
```html
339
-
<divid="v-for-if"class="demo">
340
-
<divv-for="item in list"v-if="item"><div>{{ item }}</div></div>
341
-
<divv-for="item2 in list2"v-else><div>{{ item2 }}</div></div>
342
-
</div>
343
-
```
344
-
345
-
```js
346
-
newVue({
347
-
el:'#v-for-if',
348
-
data: {
349
-
list: [0, false, 1],
350
-
list2: [4, 5, 6]
351
-
}
352
-
})
336
+
```html
337
+
<liv-for="todo in todos"v-if="!todo.isComplete">
338
+
{{ todo }}
339
+
</li>
353
340
```
354
341
355
-
Because `v-for` has higher priority than `v-if`, Vue will loop item in the list first. If item is true, it will output `item` value, else it will loop list2 and output the items of it.
356
-
357
-
So the result of the above example code will be:
358
-
359
-
{% raw %}
360
-
<divid="v-for-if"class="demo">
361
-
<divv-for="item in list"v-if="item"><div>{{ item }}</div></div>
362
-
<divv-for="item2 in list2"v-else><div>{{ item2 }}</div></div>
363
-
</div>
364
-
<script>
365
-
newVue({
366
-
el:'#v-for-if',
367
-
data: {
368
-
list: [0, false, 1],
369
-
list2: [4, 5, 6]
370
-
}
371
-
})
372
-
</script>
373
-
{% endraw %}
374
-
375
-
Let's see a complicated example. This example shows the case that v-for, v-if, v-else-if and v-else used together.
342
+
The above only renders the todos that are not complete.
376
343
377
-
```html
378
-
<divid="v-for-if-complicated"class="demo">
379
-
<divv-for="item in list"v-if="item"><div>{{ item }}</div></div>
380
-
<divv-for="item2 in list2"v-else-if="item === false"><div>{{ item2 }}</div></div>
381
-
<divv-else><div>else</div></div>
382
-
</div>
383
-
```
344
+
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.html#Conditional-Groups-with-v-if-on-lt-template-gt)). For example:
384
345
385
-
```js
386
-
newVue({
387
-
el:'#v-for-if-complicated',
388
-
data: {
389
-
list: [0, false, 1],
390
-
list2: [4, 5, 6]
391
-
}
392
-
})
346
+
```html
347
+
<ulv-if="shouldRenderTodos">
348
+
<liv-for="todo in todos">
349
+
{{ todo }}
350
+
</li>
351
+
</ul>
393
352
```
394
353
395
-
Result:
396
-
397
-
{% raw %}
398
-
<divid="v-for-if-complicated"class="demo">
399
-
<divv-for="item in list"v-if="item"><div>{{ item }}</div></div>
400
-
<divv-for="item2 in list2"v-else-if="item === false"><div>{{ item2 }}</div></div>
401
-
<divv-else><div>else</div></div>
402
-
</div>
403
-
<script>
404
-
newVue({
405
-
el:'#v-for-if-complicated',
406
-
data: {
407
-
list: [
6AD8
0, false, 1],
408
-
list2: [4, 5, 6]
409
-
}
410
-
})
411
-
</script>
412
-
{% endraw %}
413
-
414
354
## `key`
415
355
416
356
When Vue is updating a list of elements rendered with `v-for`, it by default uses an "in-place patch" strategy. If the order of the data items has changed, instead of moving the DOM elements to match the order of the items, Vue will simply patch each element in-place and make sure it reflects what should be rendered at that particular index. This is similar to the behavior of `track-by="$index"` in Vue 1.x.
0 commit comments