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: source/guide/list.md
+32-32Lines changed: 32 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -3,9 +3,9 @@ type: guide
3
3
order: 5
4
4
---
5
5
6
-
You can use the `v-repeat`directive to repeat a template element based on an Array of objects on the ViewModel. For every object in the Array, the directive will create a child Vue instance using that object as its `$data`object. These child instances inherit all data on the parent, so in the repeated element you have access to properties on both the repeated instance and the parent instance. In addition, you get access to the `$index`property, which will be the corresponding Array index of the rendered instance.
Sometimes we might want to have more explicit variable access instead of implicitly falling back to parent scope. You can do that by providing an argument to the `v-repeat`directive and use it as the identifier for the item being iterated:
Under the hood, Vue.js intercepts an observed Array's mutating methods (`push()`, `pop()`, `shift()`, `unshift()`, `splice()`, `sort()`and `reverse()`) so they will also trigger View updates.
Vue.js augments observed Arrays with two convenience methods: `$set()`and`$remove()`.
130
+
Vue.js 给观察数组添加了两个便捷方法:`$set()`和`$remove()` 。
131
131
132
-
You should avoid directly setting elements of a data-bound Array with indices, because those changes will not be picked up by Vue.js. Instead, use the agumented `$set()`method:
// same as `demo.items[0] = ...` but triggers view update
136
136
demo.items.$set(0, { childMsg:'Changed!'})
137
137
```
138
138
139
-
`$remove()`is just syntax sugar for `splice()`. It will remove the element at the given index. When the argument is not a number, `$remove()`will search for that value in the array and remove the first occurrence.
When you are using non-mutating methods, e.g. `filter()`, `concat()`or`slice()`, the returned Array will be a different instance. In that case, you can just replace the old Array with the new one:
You might think this will blow away the existing DOM and re-build everything. But worry not - Vue.js recognizes array elements that already have an associated Vue instance and will reuse those instances whenever possible.
In some cases, you might need to replace the Array with completely new objects - e.g. ones returned from an API call. If your data objects have a unique id property, then you can use a `track-by`attribute to give Vue.js a hint so that it can reuse an existing instance with data that has the same id.
@@ -170,17 +170,17 @@ For example, if your data looks like this:
170
170
}
171
171
```
172
172
173
-
Then you can give the hint like this:
173
+
那么你可以像这样给出提示:
174
174
175
175
```html
176
176
<divv-repeat="items"track-by="_uid">
177
177
<!-- content -->
178
178
</div>
179
179
```
180
180
181
-
## Iterating Through An Object
181
+
## 遍历对象
182
182
183
-
You can also use `v-repeat`to iterate through the properties of an Object. Each repeated instance will have a special property `$key`. For primitive values, you also get `$value`which is similar to primitive values in Arrays.
<pclass="tip">In ECMAScript 5 there is no way to detect when a new property is added to an Object, or when a property is deleted from an Object. To deal with that, observed objects will be augmented with two methods: `$add(key, value)` and `$delete(key)`. These methods can be used to add / delete properties from observed objects while triggering the desired View updates.</p>
Sometimes we only need to display a filtered or sorted version of the Array without actually mutating or resetting the original data. Vue provides two built-in filters to simplify such usage: `filterBy`and`orderBy`. Check out their [documentations](../api/filters.html#filterBy) for more details.
0 commit comments