Be able to use string type index in array (#5889)

This commit is contained in:
王斐 2017-06-16 09:37:31 +08:00 committed by Evan You
parent 080c387d49
commit 8a2c5147ad
2 changed files with 19 additions and 1 deletions

View File

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

View File

@ -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)
})
})
})