Skip to content

Commit 4103029

Browse files
chrisvfritzsdras
authored andcommitted
Clarify key in list rendering doc, fixes vuejs#1540 (vuejs#1554)
* clarify key in list rendering doc * further clarify key distinction in list guide * make usage of key/name/index more consistent
1 parent 495f3df commit 4103029

File tree

6 files changed

+47
-44
lines changed

6 files changed

+47
-44
lines changed

src/v2/api/index.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ type: api
246246
247247
- **See also:** [Async Update Queue](../guide/reactivity.html#Async-Update-Queue)
248248

249-
### Vue.set( target, key, value )
249+
### Vue.set( target, propertyName/index, value )
250250

251251
- **Arguments:**
252252
- `{Object | Array} target`
253-
- `{string | number} key`
253+
- `{string | number} propertyName/index`
254254
- `{any} value`
255255

256256
- **Returns:** the set value.
@@ -263,11 +263,11 @@ type: api
263263

264264
- **See also:** [Reactivity in Depth](../guide/reactivity.html)
265265

266-
### Vue.delete( target, key )
266+
### Vue.delete( target, propertyName/index )
267267

268268
- **Arguments:**
269269
- `{Object | Array} target`
270-
- `{string | number} key/index`
270+
- `{string | number} propertyName/index`
271271

272272
> Only in 2.2.0+: Also works with Array + index.
273273
@@ -1576,11 +1576,11 @@ type: api
15761576
// `callback` is fired immediately with current value of `a`
15771577
```
15781578
1579-
### vm.$set( target, key, value )
1579+
### vm.$set( target, propertyName/index, value )
15801580
15811581
- **Arguments:**
15821582
- `{Object | Array} target`
1583-
- `{string | number} key`
1583+
- `{string | number} propertyName/index`
15841584
- `{any} value`
15851585
15861586
- **Returns:** the set value.
@@ -1591,11 +1591,11 @@ type: api
15911591
15921592
- **See also:** [Vue.set](#Vue-set)
15931593
1594-
### vm.$delete( target, key )
1594+
### vm.$delete( target, propertyName/index )
15951595
15961596
- **Arguments:**
15971597
- `{Object | Array} target`
1598-
- `{string | number} key`
1598+
- `{string | number} propertyName/index`
15991599
16001600
- **Usage:**
16011601
@@ -2013,7 +2013,7 @@ type: api
20132013
``` html
20142014
<div v-for="(item, index) in items"></div>
20152015
<div v-for="(val, key) in object"></div>
2016-
<div v-for="(val, key, index) in object"></div>
2016+
<div v-for="(val, name, index) in object"></div>
20172017
```
20182018

20192019
The default behavior of `v-for` will try to patch the elements in-place without moving them. To force it to reorder elements, you need to provide an ordering hint with the `key` special attribute:

src/v2/guide/list.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ new Vue({
117117
el: '#v-for-object',
118118
data: {
119119
object: {
120-
firstName: 'John',
121-
lastName: 'Doe',
122-
age: 30
120+
title: 'How to do lists in Vue',
121+
author: 'Jane Doe',
122+
publishedAt: '2016-04-10'
123123
}
124124
}
125125
})
@@ -138,37 +138,37 @@ new Vue({
138138
el: '#v-for-object',
139139
data: {
140140
object: {
141-
firstName: 'John',
142-
lastName: 'Doe',
143-
age: 30
141+
title: 'How to do lists in Vue',
142+
author: 'Jane Doe',
143+
publishedAt: '2016-04-10'
144144
}
145145
}
146146
})
147147
</script>
148148
{% endraw %}
149149

150-
You can also provide a second argument for the key:
150+
You can also provide a second argument for the property's name (a.k.a. key):
151151

152152
``` html
153-
<div v-for="(value, key) in object">
154-
{{ key }}: {{ value }}
153+
<div v-for="(value, name) in object">
154+
{{ name }}: {{ value }}
155155
</div>
156156
```
157157

158158
{% raw %}
159-
<div id="v-for-object-value-key" class="demo">
160-
<div v-for="(value, key) in object">
161-
{{ key }}: {{ value }}
159+
<div id="v-for-object-value-name" class="demo">
160+
<div v-for="(value, name) in object">
161+
{{ name }}: {{ value }}
162162
</div>
163163
</div>
164164
<script>
165165
new Vue({
166-
el: '#v-for-object-value-key',
166+
el: '#v-for-object-value-name',
167167
data: {
168168
object: {
169-
firstName: 'John',
170-
lastName: 'Doe',
171-
age: 30
169+
title: 'How to do lists in Vue',
170+
author: 'Jane Doe',
171+
publishedAt: '2016-04-10'
172172
}
173173
}
174174
})
@@ -178,48 +178,48 @@ new Vue({
178178
And another for the index:
179179

180180
``` html
181-
<div v-for="(value, key, index) in object">
182-
{{ index }}. {{ key }}: {{ value }}
181+
<div v-for="(value, name, index) in object">
182+
{{ index }}. {{ name }}: {{ value }}
183183
</div>
184184
```
185185

186186
{% raw %}
187-
<div id="v-for-object-value-key-index" class="demo">
188-
<div v-for="(value, key, index) in object">
189-
{{ index }}. {{ key }}: {{ value }}
187+
<div id="v-for-object-value-name-index" class="demo">
188+
<div v-for="(value, name, index) in object">
189+
{{ index }}. {{ name }}: {{ value }}
190190
</div>
191191
</div>
192192
<script>
193193
new Vue({
194-
el: '#v-for-object-value-key-index',
194+
el: '#v-for-object-value-name-index',
195195
data: {
196196
object: {
197-
firstName: 'John',
198-
lastName: 'Doe',
199-
age: 30
197+
title: 'How to do lists in Vue',
198+
author: 'Jane Doe',
199+
publishedAt: '2016-04-10'
200200
}
201201
}
202202
})
203203
</script>
204204
{% endraw %}
205205

206-
<p class="tip">When iterating over an object, the order is based on the key enumeration order of `Object.keys()`, which is **not** guaranteed to be consistent across JavaScript engine implementations.</p>
206+
<p class="tip">When iterating over an object, the order is based on the enumeration order of `Object.keys()`, which is **not** guaranteed to be consistent across JavaScript engine implementations.</p>
207207

208-
## `key`
208+
## Maintaining State
209209

210210
When Vue is updating a list of elements rendered with `v-for`, by default it 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 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.
211211

212-
This default mode is efficient, but only suitable **when your list render output does not rely on child component state or temporary DOM state (e.g. form input values)**.
212+
This default mode is efficient, but **only suitable when your list render output does not rely on child component state or temporary DOM state (e.g. form input values)**.
213213

214-
To give Vue a hint so that it can track each node's identity, and thus reuse and reorder existing elements, you need to provide a unique `key` attribute for each item. An ideal value for `key` would be the unique id of each item. This special attribute is a rough equivalent to `track-by` in 1.x, but it works like an attribute, so you need to use `v-bind` to bind it to dynamic values (using shorthand here):
214+
To give Vue a hint so that it can track each node's identity, and thus reuse and reorder existing elements, you need to provide a unique `key` attribute for each item:
215215

216216
``` html
217-
<div v-for="item in items" :key="item.id">
217+
<div v-for="item in items" v-bind:key="item.id">
218218
<!-- content -->
219219
</div>
220220
```
221221

222-
It is recommended to provide a `key` with `v-for` whenever possible, unless the iterated DOM content is simple, or you are intentionally relying on the default behavior for performance gains.
222+
It is recommended to provide a `key` attribute with `v-for` whenever possible, unless the iterated DOM content is simple, or you are intentionally relying on the default behavior for performance gains.
223223

224224
Since it's a generic mechanism for Vue to identify nodes, the `key` also has other uses that are not specifically tied to `v-for`, as we will see later in the guide.
225225

@@ -313,7 +313,7 @@ vm.b = 2
313313
// `vm.b` is NOT reactive
314314
```
315315

316-
Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it's possible to add reactive properties to a nested object using the `Vue.set(object, key, value)` method. For example, given:
316+
Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it's possible to add reactive properties to a nested object using the `Vue.set(object, propertyName, value)` method. For example, given:
317317

318318
``` js
319319
var vm = new Vue({

src/v2/guide/migration-vue-router.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var router = new VueRouter({
7373
})
7474
```
7575

76-
The array syntax allows more predictable route matching, since iterating over an object is not guaranteed to use the same key order across browsers.
76+
The array syntax allows more predictable route matching, since iterating over an object is not guaranteed to use the same property order across browsers.
7777

7878
{% raw %}
7979
<div class="upgrade-path">

src/v2/guide/migration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ When including an `index`, the argument order for arrays used to be `(index, val
186186

187187
### `v-for` Argument Order for Objects <sup>changed</sup>
188188

189-
When including a `key`, the argument order for objects used to be `(key, value)`. It is now `(value, key)` to be more consistent with common object iterators such as lodash's.
189+
When including a property name/key, the argument order for objects used to be `(name, value)`. It is now `(value, name)` to be more consistent with common object iterators such as lodash's.
190190

191191
{% raw %}
192192
<div class="upgrade-path">

src/v2/guide/reactivity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ vm.b = 2
3434
// `vm.b` is NOT reactive
3535
```
3636

37-
Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it's possible to add reactive properties to a nested object using the `Vue.set(object, key, value)` method:
37+
Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it's possible to add reactive properties to a nested object using the `Vue.set(object, propertyName, value)` method:
3838

3939
``` js
4040
Vue.set(vm.someObject, 'b', 2)

themes/vue/source/js/common.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
// moved to a perhaps differently-named section on
2121
// another page, we need this.
2222
function initHashLevelRedirects() {
23+
checkForHashRedirect(/list\.html$/, {
24+
key: '/v2/guide/list.html#Maintaining-State'
25+
})
2326
checkForHashRedirect(/components\.html$/, {
2427
'What-are-Components': '/v2/guide/components.html',
2528
'Using-Components': '/v2/guide/components-registration.html',

0 commit comments

Comments
 (0)