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
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