mirror of https://github.com/vuejs/vue.git
Be able to use string type index in array (#5889)
This commit is contained in:
parent
080c387d49
commit
8a2c5147ad
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue