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/v2/guide/list.md
+1-97Lines changed: 1 addition & 97 deletions
Original file line number
Diff line number
Diff line change
@@ -260,103 +260,7 @@ You might think this will cause Vue to throw away the existing DOM and re-render
260
260
261
261
### Caveats
262
262
263
-
Due to limitations in JavaScript, Vue **cannot** detect the following changes to an array:
264
-
265
-
1. When you directly set an item with the index, e.g. `vm.items[indexOfItem] = newValue`
266
-
2. When you modify the length of the array, e.g. `vm.items.length = newLength`
267
-
268
-
For example:
269
-
270
-
```js
271
-
var vm =newVue({
272
-
data: {
273
-
items: ['a', 'b', 'c']
274
-
}
275
-
})
276
-
vm.items[1] ='x'// is NOT reactive
277
-
vm.items.length=2// is NOT reactive
278
-
```
279
-
280
-
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:
281
-
282
-
```js
283
-
// Vue.set
284
-
Vue.set(vm.items, indexOfItem, newValue)
285
-
```
286
-
```js
287
-
// Array.prototype.splice
288
-
vm.items.splice(indexOfItem, 1, newValue)
289
-
```
290
-
291
-
You can also use the [`vm.$set`](https://vuejs.org/v2/api/#vm-set) instance method, which is an alias for the global `Vue.set`:
292
-
293
-
```js
294
-
vm.$set(vm.items, indexOfItem, newValue)
295
-
```
296
-
297
-
To deal with caveat 2, you can use `splice`:
298
-
299
-
```js
300
-
vm.items.splice(newLength)
301
-
```
302
-
303
-
## Object Change Detection Caveats
304
-
305
-
Again due to limitations of modern JavaScript, **Vue cannot detect property addition or deletion**. For example:
306
-
307
-
```js
308
-
var vm =newVue({
309
-
data: {
310
-
a:1
311
-
}
312
-
})
313
-
// `vm.a` is now reactive
314
-
315
-
vm.b=2
316
-
// `vm.b` is NOT reactive
317
-
```
318
-
319
-
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:
320
-
321
-
```js
322
-
var vm =newVue({
323
-
data: {
324
-
userProfile: {
325
-
name:'Anika'
326
-
}
327
-
}
328
-
})
329
-
```
330
-
331
-
You could add a new `age` property to the nested `userProfile` object with:
332
-
333
-
```js
334
-
Vue.set(vm.userProfile, 'age', 27)
335
-
```
336
-
337
-
You can also use the `vm.$set` instance method, which is an alias for the global `Vue.set`:
338
-
339
-
```js
340
-
vm.$set(vm.userProfile, 'age', 27)
341
-
```
342
-
343
-
Sometimes you may want to assign a number of new properties to an existing object, for example using `Object.assign()` or `_.extend()`. In such cases, you should create a fresh object with properties from both objects. So instead of:
Due to limitations in JavaScript, there are types of changes that Vue **cannot detect** with arrays and objects. These are discussed in the [reactivity](reactivity.html#Change-Detection-Caveats) section.
Copy file name to clipboardExpand all lines: src/v2/guide/reactivity.md
+46-2Lines changed: 46 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,11 @@ Every component instance has a corresponding **watcher** instance, which records
20
20
21
21
## Change Detection Caveats
22
22
23
-
Due to the limitations of modern JavaScript (and the abandonment of `Object.observe`), Vue **cannot detect property addition or deletion**. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the `data` object in order for Vue to convert it and make it reactive. For example:
23
+
Due to limitations in JavaScript, there are types of changes that Vue **cannot detect**. However, there are ways to circumvent them to preserve reactivity.
24
+
25
+
### For Objects
26
+
27
+
Vue cannot detect property addition or deletion. Since Vue performs the getter/setter conversion process during instance initialization, a property must be present in the `data` object in order for Vue to convert it and make it reactive. For example:
24
28
25
29
```js
26
30
var vm =newVue({
@@ -53,7 +57,47 @@ Sometimes you may want to assign a number of properties to an existing object, f
There are also a few array-related caveats, which were discussed earlier in the [list rendering section](list.html#Caveats).
60
+
### For Arrays
61
+
62
+
Vue cannot detect the following changes to an array:
63
+
64
+
1. When you directly set an item with the index, e.g. `vm.items[indexOfItem] = newValue`
65
+
2. When you modify the length of the array, e.g. `vm.items.length = newLength`
66
+
67
+
For example:
68
+
69
+
```js
70
+
var vm =newVue({
71
+
data: {
72
+
items: ['a', 'b', 'c']
73
+
}
74
+
})
75
+
vm.items[1] ='x'// is NOT reactive
76
+
vm.items.length=2// is NOT reactive
77
+
```
78
+
79
+
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:
80
+
81
+
```js
82
+
// Vue.set
83
+
Vue.set(vm.items, indexOfItem, newValue)
84
+
```
85
+
```js
86
+
// Array.prototype.splice
87
+
vm.items.splice(indexOfItem, 1, newValue)
88
+
```
89
+
90
+
You can also use the [`vm.$set`](https://vuejs.org/v2/api/#vm-set) instance method, which is an alias for the global `Vue.set`:
0 commit comments