Skip to content

Commit 50a9b9d

Browse files
authored
Merge pull request #41 from tingtien/2.0-cn
List Rendering已翻译完
2 parents 6d99e9b + 05c91b3 commit 50a9b9d

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

src/guide/list.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ order: 8
66

77
## `v-for`
88

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` 是数组元素迭代的别名。
1010

1111
### 基本用法
1212

1313
``` html
1414
<ul id="example-1">
15-
<li v-for="item in items">
16-
{{ item.message }}
17-
</li>
15+
<li v-for="item in items">
16+
{{ item.message }}
17+
</li>
1818
</ul>
1919
```
2020

2121
``` js
2222
var example1 = new Vue({
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+
}
3030
})
3131
```
3232

33-
Result:
33+
结果:
3434

3535
{% raw %}
3636
<ul id="example-1" class="demo">
@@ -56,7 +56,7 @@ var example1 = new Vue({
5656
</script>
5757
{% endraw %}
5858

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.
59+
`v-for` 块内部,我们有完全访问父作用域的属性。`v-for` 还支持一个可选的第二个参数为当前项的索引。
6060

6161
``` html
6262
<ul id="example-2">
@@ -79,7 +79,7 @@ var example2 = new Vue({
7979
})
8080
```
8181

82-
Result:
82+
结果:
8383

8484
{% raw%}
8585
<ul id="example-2" class="demo">
@@ -106,15 +106,15 @@ var example2 = new Vue({
106106
</script>
107107
{% endraw %}
108108

109-
You can also use `of` as the delimiter instead of `in`, so that it is closer to JavaScript's syntax for iterators:
109+
你也可以用 `of` 替代 `in` 作为分隔符, 所以它是最接近JavaScript迭代器的语法
110110

111111
``` html
112112
<div v-for="item of items"></div>
113113
```
114114

115115
### Template v-for
116116

117-
Similar to template `v-if`, you can also use a `<template>` tag with `v-for` to render a block of multiple elements. For example:
117+
如同 `v-if` 模板,你也可以用带有 `v-for``<template>` 标签来渲染多个元素块。例如:
118118

119119
``` html
120120
<ul>
@@ -125,9 +125,9 @@ Similar to template `v-if`, you can also use a `<template>` tag with `v-for` to
125125
</ul>
126126
```
127127

128-
### Object v-for
128+
### 对象迭代 v-for
129129

130-
You can also use `v-for` to iterate through the properties of an object.
130+
你也可以用 `v-for` 通过一个对象的属性来迭代。
131131

132132
``` html
133133
<ul id="repeat-object" class="demo">
@@ -150,7 +150,7 @@ new Vue({
150150
})
151151
```
152152

153-
Result:
153+
结果:
154154

155155
{% raw %}
156156
<ul id="repeat-object" class="demo">
@@ -172,35 +172,35 @@ new Vue({
172172
</script>
173173
{% endraw %}
174174

175-
You can also provide a second argument for the key:
175+
你也可以提供第二个的参数为键名:
176176

177177
``` html
178178
<div v-for="(value, key) in object">
179179
{{ key }} : {{ value }}
180180
</div>
181181
```
182182

183-
And another for the index:
183+
第三个参数为索引:
184184

185185
``` html
186186
<div v-for="(value, key, index) in object">
187187
{{ index }}. {{ key }} : {{ value }}
188188
</div>
189189
```
190190

191-
<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>
191+
<p class="tip">当遍历一个对象时,顺序是根据 `Object.keys()` 可枚举键名排列,不保证和JavaScript引擎实现是一致的。</p>
192192

193-
### Range v-for
193+
### 整数迭代 v-for
194194

195-
`v-for` can also take an integer. In this case it will repeat the template that many times.
195+
`v-for` 也可以取整数。在这种情况下,它将重复多次模板
196196

197197
``` html
198198
<div>
199199
<span v-for="n in 10">{{ n }}</span>
200200
</div>
201201
```
202202

203-
Result:
203+
结果:
204204

205205
{% raw %}
206206
<div id="range" class="demo">
@@ -211,17 +211,17 @@ new Vue({ el: '#range' })
211211
</script>
212212
{% endraw %}
213213

214-
### Components and v-for
214+
### 组件 和 v-for
215215

216-
> This section assumes knowledge of [Components](/guide/components.html). Feel free to skip it and come back later.
216+
> 了解组件相关知识,查看 [组件](/guide/components.html)
217217
218-
You can directly use `v-for` on a custom component, like any normal element:
218+
在自定义组件里,你可以像任何普通元素一样用 `v-for`
219219

220220
``` html
221221
<my-component v-for="item in items"></my-component>
222222
```
223223

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:
224+
然而他不能自动传递数据到组件里,因为组件有自己独立的作用域。为了传递迭代数据到组件里,我们要用 `props`
225225

226226
``` html
227227
<my-component
@@ -231,9 +231,9 @@ However, this won't automatically pass any data to the component, because compon
231231
</my-component>
232232
```
233233

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.
234+
不自动注入 `item` 到组件里是因为这样能让组件紧密耦合 `v-for` 怎么运作的。在一些情况下,明确了数据的来源让其组件可重复使用。
235235

236-
Here's a complete example of a simple todo list:
236+
下面是一个简单的todo list完整的例子:
237237

238238
``` html
239239
<div id="todo-list-example">
@@ -331,27 +331,27 @@ new Vue({
331331

332332
## key
333333

334-
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.
334+
当Vue.js用 `v-for` 正在更新已渲染过的元素列表时,它默认用 “就地复用” 策略。如果数据项的顺序被改变,而不是移动DOM元素来匹配列表的顺序,Vue将简单复用此处每个元素,并且确保它在特定索引下显示已被渲染过的每个元素。这个类似Vue 1.X的 `track-by="$index"`
335335

336-
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)**.
336+
这个默认的模式是有效的,但是只适合你的列表渲染输出时不依赖子组件状态或临时DOM状态(例如:表单输入框的值)
337337

338-
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):
338+
给Vue一个提示以便它能跟踪每个节点身份,并且因此重新使用和重新排序现有元素,你需要为每项提供一个唯一 `key` 属性。一个有效的 `key` 值是每项唯一ID. 这个特殊的属性相当于Vue 1.x的 `track-by` ,但它像一个变量,所以你需要用 `v-bind` 来绑定动态值(简洁写法如下)
339339

340340
``` html
341341
<div v-for="item in items" :key="item.id">
342342
<!-- content -->
343343
</div>
344344
```
345345

346-
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.
346+
它建议尽可能用 `v-for` 时提供一个 `key` 。除非迭代DOM内容很简单,或者你有意要依赖于对性能提升的默认行为。
347347

348-
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.
348+
因为它是Vue识别节点的通用的机制,`key` 还具有并不特别依赖于 `v-for` 的其他用途,我们将在后面的指南中看到其他用途。
349349

350-
## Array Change Detection
350+
## 数组更新检测
351351

352-
### Mutation Methods
352+
### 突变方法
353353

354-
Vue wraps an observed array's mutation methods so they will also trigger view updates. The wrapped methods are:
354+
Vue包含一组观察数组的突变方法,所以它们也将会触发视图更新。这些方法如下:
355355

356356
- `push()`
357357
- `pop()`
@@ -361,28 +361,28 @@ Vue wraps an observed array's mutation methods so they will also trigger view up
361361
- `sort()`
362362
- `reverse()`
363363

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' })`.
364+
你打开控制台,然后用前面例子的 `items` 数组调用突变方法:`example1.items.push({ measage: 'Baz' })`
365365

366-
### Replacing an Array
366+
### 重塑数组
367367

368-
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:
368+
突变方法,顾名思义,改变原始数组的称呼。相比之下,也有不改变原组数的方法,例如:`filter()``concat()` `slice()` 。这些不会改变原始数组,但总是返回一个新的数组。当用不改变原数组的方法时,你只需替换旧数组用新的:
369369

370370
``` js
371371
example1.items = example1.items.filter(function (item) {
372372
return item.message.match(/Foo/)
373373
})
374374
```
375375

376-
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.
376+
你可你能认为这将导致Vue丢掉现有DOM和重新渲染整个列表。幸运的是,事实并非如此。Vue实现了一些智能化去最大化的重用DOM元素,所以用另外含有重叠对象的数组替换数组是非常高效的操作。
377377

378-
### Caveats
378+
### 注意事项
379379

380-
Due to limitations in JavaScript, Vue **cannot** detect the following changes to an array:
380+
由于JavaScript的限制,Vue不能检测以下变动的数组:
381381

382-
1. When you directly set an item with the index, e.g. `vm.items[indexOfItem] = newValue`
383-
2. When you modify the length of the array, e.g. `vm.items.length = newLength`
382+
1. 当你直接设置一个项的索引时, 例如: `vm.items[indexOfItem] = newValue`
383+
2. 当你修改数组的长度时, 例如: `vm.items.length = newLength`
384384

385-
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:
385+
为了避免第一种情况, 以下两种方式将达到像 `vm.items[indexOfItem] = newValue` 的效果, 同时也将触发状态更新:
386386

387387
``` js
388388
// Vue.set
@@ -393,17 +393,17 @@ Vue.set(example1.items, indexOfItem, newValue)
393393
example1.items.splice(indexOfItem, 1, newValue)
394394
```
395395

396-
To deal with caveat 2, you can also use `splice`:
396+
避免第二种情况, 使用 `splice`:
397397

398398
``` js
399399
example1.items.splice(newLength)
400400
```
401401

402-
## Displaying Filtered/Sorted Results
402+
## 显示过滤/排序结果
403403

404-
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.
404+
有时我们无需实际改变或重置原始数据对数组进行过滤或排序。在这种情况下,你可以创建计算的属性返回过滤或排序的数组。
405405

406-
For example:
406+
例如:
407407

408408
``` html
409409
<li v-for="n in evenNumbers">{{ n }}</li>
@@ -422,7 +422,7 @@ computed: {
422422
}
423423
```
424424

425-
Alternatively, you can also just use a method where computed properties are not feasible (e.g. inside nested `v-for` loops):
425+
此外, 你也可以只使用一种方法计算 (例如: 嵌套在 `v-for` 里循环),但是性能是低效的:
426426

427427
``` html
428428
<li v-for="n in even(numbers)">{{ n }}</li>

0 commit comments

Comments
 (0)