Skip to content

Update list.md #1429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/v2/guide/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,39 @@ Due to limitations in JavaScript, Vue **cannot** detect the following changes to
1. When you directly set an item with the index, e.g. `vm.items[indexOfItem] = newValue`
2. When you modify the length of the array, e.g. `vm.items.length = newLength`

For example:

``` js
var vm = new Vue({
data: {
items: ['a', 'b', 'c']
}
})
vm.items[1] = 'x' // is NOT reactive
vm.items.length = 2 // is NOT reactive
```

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:

``` js
// Vue.set
Vue.set(example1.items, indexOfItem, newValue)
Vue.set(vm.items, indexOfItem, newValue)
```
``` js
// Array.prototype.splice
example1.items.splice(indexOfItem, 1, newValue)
vm.items.splice(indexOfItem, 1, newValue)
```

You can also use the `vm.$set` instance method, which is an alias for the global `Vue.set`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a link to the API docs to be included: https://vuejs.org/v2/api/#vm-set

Copy link
Contributor Author

@01abhishekjain 01abhishekjain Mar 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea, especially since vm.$set is also talked about later, in the Object change detection caveats section.

EDIT:
Added a new commit for this 🤞


``` js
vm.$set(vm.items, indexOfItem, newValue)
```

To deal with caveat 2, you can use `splice`:

``` js
example1.items.splice(newLength)
vm.items.splice(newLength)
```

## Object Change Detection Caveats
Expand Down