Skip to content

Commit

Permalink
Be able to use string type index in array (#5889)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter-WF authored and yyx990803 committed Jun 16, 2017
1 parent 080c387 commit 8a2c514
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function defineReactive (
* already exist.
*/
export function set (target: Array<any> | Object, key: any, val: any): any {
if (Array.isArray(target) && typeof key === 'number') {
if (Array.isArray(target) && (typeof key === 'number' || /^\d+$/.test(key))) {
target.length = Math.max(target.length, key)
target.splice(key, 1, val)
return val
Expand Down
18 changes: 18 additions & 0 deletions test/unit/features/global-api/set-delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,23 @@ describe('Global API: set/delete', () => {
expect(vm.$el.innerHTML).toBe('')
}).then(done)
})

it('be able to use string type index in array', done => {
const vm = new Vue({
template: '<div><p v-for="obj in lists">{{obj.name}}</p></div>',
data: {
lists: [
{ name: 'A' },
{ name: 'B' },
{ name: 'C' }
]
}
}).$mount()
expect(vm.$el.innerHTML).toBe('<p>A</p><p>B</p><p>C</p>')
Vue.set(vm.lists, '0', { name: 'D' })
waitForUpdate(() => {
expect(vm.$el.innerHTML).toBe('<p>D</p><p>B</p><p>C</p>')
}).then(done)
})
})
})

0 comments on commit 8a2c514

Please sign in to comment.