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
* keyCodes now support array type
* remove trailing whitespace
* add more info about v-for with v-if
* update code example
* fix typo
* fix typo
* Update links to list rendering guide
* Update copy about v-if with -vofr on conditional guide
Copy file name to clipboardExpand all lines: src/v2/api/index.md
+5-1Lines changed: 5 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1328,6 +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>
@@ -1411,7 +1413,9 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
1411
1413
{{ item.text }}
1412
1414
</div>
1413
1415
```
1414
-
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
+
1415
1419
The detailed usage for `v-for` is explained in the guide section linked below.
Copy file name to clipboardExpand all lines: src/v2/guide/conditional.md
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -193,3 +193,7 @@ The difference is that an element with `v-show` will always be rendered and rema
193
193
In comparison, `v-show` is much simpler - the element is always rendered regardless of initial condition, with just simple CSS-based toggling.
194
194
195
195
Generally speaking, `v-if` has higher toggle costs while `v-show` has higher initial render costs. So prefer `v-show` if you need to toggle something very often, and prefer `v-if` if the condition is unlikely to change at runtime.
196
+
197
+
## `v-if` with `v-for`
198
+
199
+
When used together with `v-for`, `v-for` has a higher priority than `v-if`. See the <ahref="../guide/list.html#V-for-and-v-if">list rendering guide</a> for details.
Copy file name to clipboardExpand all lines: src/v2/guide/list.md
+84-1Lines changed: 84 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -329,7 +329,90 @@ new Vue({
329
329
</script>
330
330
{% endraw %}
331
331
332
-
## key
332
+
### V-for and v-if
333
+
334
+
In Vue.js template, v-for has a higher priority than v-if when they exists in the same node.
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
+
})
353
+
```
354
+
355
+
356
+
Because v-for has higher priority than v-if, Vue.js 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.
357
+
358
+
So the result of the above example code will be:
359
+
360
+
{% raw %}
361
+
<divid="v-for-if"class="demo">
362
+
<divv-for="item in list"v-if="item"><div>{{ item }}</div></div>
363
+
<divv-for="item2 in list2"v-else><div>{{ item2 }}</div></div>
364
+
</div>
365
+
<script>
366
+
newVue({
367
+
el:'#v-for-if',
368
+
data: {
369
+
list: [0, false, 1],
370
+
list2: [4, 5, 6]
371
+
}
372
+
})
373
+
</script>
374
+
{% endraw %}
375
+
376
+
Let's see a complicated example. This example shows the case that v-for, v-if, v-else-if and v-else used together.
377
+
378
+
```html
379
+
<divid="v-for-if-complicated"class="demo">
380
+
<divv-for="item in list"v-if="item"><div>{{ item }}</div></div>
381
+
<divv-for="item2 in list2"v-else-if="item === false"><div>{{ item2 }}</div></div>
382
+
<divv-else><div>else</div></div>
383
+
</div>
384
+
```
385
+
386
+
```js
387
+
newVue({
388
+
el:'#v-for-if-complicated',
389
+
data: {
390
+
list: [0, false, 1],
391
+
list2: [4, 5, 6]
392
+
}
393
+
})
394
+
```
395
+
396
+
Result:
397
+
398
+
{% raw %}
399
+
<divid="v-for-if-complicated"class="demo">
400
+
<divv-for="item in list"v-if="item"><div>{{ item }}</div></div>
401
+
<divv-for="item2 in list2"v-else-if="item === false"><div>{{ item2 }}</div></div>
402
+
<divv-else><div>else</div></div>
403
+
</div>
404
+
<script>
405
+
newVue({
406
+
el:'#v-for-if-complicated',
407
+
data: {
408
+
list: [0, false, 1],
409
+
list2: [4, 5, 6]
410
+
}
411
+
})
412
+
</script>
413
+
{% endraw %}
414
+
415
+
## Key
333
416
334
417
When Vue.js 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