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/list.md
+53-53Lines changed: 53 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -6,31 +6,31 @@ order: 8
6
6
7
7
## `v-for`
8
8
9
-
We can use the `v-for`directive to render a list of items based on an array. The `v-for`directive requires a special syntax in the form of `item in items`, where `items`is the source data array and `item`is an **alias** for the array element being iterated on:
9
+
我们用`v-for`指令根据一组数组的选项列表进行渲染。`v-for`指令需要以 `item in items` 形式的特殊语法,`items`是源数据数组并且 `item`是数组元素迭代的别名。
10
10
11
11
### 基本用法
12
12
13
13
```html
14
14
<ulid="example-1">
15
-
<liv-for="item in items">
16
-
{{ item.message }}
17
-
</li>
15
+
<liv-for="item in items">
16
+
{{ item.message }}
17
+
</li>
18
18
</ul>
19
19
```
20
20
21
21
```js
22
22
var example1 =newVue({
23
-
el:'#example-1',
24
-
data: {
25
-
items: [
26
-
{ message:'Foo' },
27
-
{ message:'Bar' }
28
-
]
29
-
}
23
+
el:'#example-1',
24
+
data: {
25
+
items: [
26
+
{message:'foo' },
27
+
{message:'Bar' }
28
+
]
29
+
}
30
30
})
31
31
```
32
32
33
-
Result:
33
+
结果:
34
34
35
35
{% raw %}
36
36
<ulid="example-1"class="demo">
@@ -56,7 +56,7 @@ var example1 = new Vue({
56
56
</script>
57
57
{% endraw %}
58
58
59
-
Inside`v-for`blocks we have full access to parent scope properties. `v-for`also supports an optional second argument for the index of the current item.
@@ -125,9 +125,9 @@ Similar to template `v-if`, you can also use a `<template>` tag with `v-for` to
125
125
</ul>
126
126
```
127
127
128
-
### Object v-for
128
+
### 对象迭代 v-for
129
129
130
-
You can also use `v-for`to iterate through the properties of an object.
130
+
你也可以用 `v-for`通过一个对象的属性来迭代。
131
131
132
132
```html
133
133
<ulid="repeat-object"class="demo">
@@ -150,7 +150,7 @@ new Vue({
150
150
})
151
151
```
152
152
153
-
Result:
153
+
结果:
154
154
155
155
{% raw %}
156
156
<ulid="repeat-object"class="demo">
@@ -172,35 +172,35 @@ new Vue({
172
172
</script>
173
173
{% endraw %}
174
174
175
-
You can also provide a second argument for the key:
175
+
你也可以提供第二个的参数为键名:
176
176
177
177
```html
178
178
<divv-for="(value, key) in object">
179
179
{{ key }} : {{ value }}
180
180
</div>
181
181
```
182
182
183
-
And another for the index:
183
+
第三个参数为索引:
184
184
185
185
```html
186
186
<divv-for="(value, key, index) in object">
187
187
{{ index }}. {{ key }} : {{ value }}
188
188
</div>
189
189
```
190
190
191
-
<pclass="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>
`v-for`can also take an integer. In this case it will repeat the template that many times.
195
+
`v-for`也可以取整数。在这种情况下,它将重复多次模板
196
196
197
197
```html
198
198
<div>
199
199
<spanv-for="n in 10">{{ n }}</span>
200
200
</div>
201
201
```
202
202
203
-
Result:
203
+
结果:
204
204
205
205
{% raw %}
206
206
<divid="range"class="demo">
@@ -211,17 +211,17 @@ new Vue({ el: '#range' })
211
211
</script>
212
212
{% endraw %}
213
213
214
-
### Components and v-for
214
+
### 组件 和 v-for
215
215
216
-
> This section assumes knowledge of [Components](/guide/components.html). Feel free to skip it and come back later.
216
+
> 了解组件相关知识,查看 [组件](/guide/components.html) 。
217
217
218
-
You can directly use `v-for`on a custom component, like any normal element:
218
+
在自定义组件里,你可以像任何普通元素一样用 `v-for`。
219
219
220
220
```html
221
221
<my-componentv-for="item in items"></my-component>
222
222
```
223
223
224
-
However, this won't automatically pass any data to the component, because components have isolated scopes of their own. In order to pass the iterated data into the component, we should also use props:
@@ -231,9 +231,9 @@ However, this won't automatically pass any data to the component, because compon
231
231
</my-component>
232
232
```
233
233
234
-
The reason for not automatically injecting `item`into the component is because that makes the component tightly coupled to how `v-for`works. Being explicit about where its data comes from makes the component reusable in other situations.
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.
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)**.
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):
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.
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.
Vue wraps an observed array's mutation methods so they will also trigger view updates. The wrapped methods are:
354
+
Vue包含一组观察数组的突变方法,所以它们也将会触发视图更新。这些方法如下:
355
355
356
356
-`push()`
357
357
-`pop()`
@@ -361,28 +361,28 @@ Vue wraps an observed array's mutation methods so they will also trigger view up
361
361
-`sort()`
362
362
-`reverse()`
363
363
364
-
You can open the console and play with the previous examples' `items`array by calling their mutation methods. For example: `example1.items.push({ message: 'Baz' })`.
Mutation methods, as the name suggests, mutate the original array they are called on. In comparison, there are also non-mutating methods, e.g. `filter()`, `concat()`and `slice()`, which do not mutate the original Array but **always return a new array**. When working with non-mutating methods, you can just replace the old array with the new one:
You might think this will cause Vue to throw away the existing DOM and re-render the entire list - luckily, that is not the case. Vue implements some smart heuristics to maximize DOM element reuse, so replacing an array with another array containing overlapping objects is a very efficient operation.
To overcome caveat 1, both of the following will accomplish the same as `vm.items[indexOfItem] = newValue`, but will also trigger state updates in the reactivity system:
Sometimes we want to display a filtered or sorted version of an array without actually mutating or resetting the original data. In this case, you can create a computed property that returns the filtered or sorted array.
0 commit comments