fix(reactivity): track length on for in iteration on Array

fix #2427
This commit is contained in:
Evan You 2020-10-19 17:37:10 -04:00
parent d9ad45ad6c
commit 0e5a3c47a7
2 changed files with 16 additions and 1 deletions

View File

@ -128,6 +128,21 @@ describe('reactivity/reactive/Array', () => {
expect(fn).toHaveBeenCalledTimes(1) expect(fn).toHaveBeenCalledTimes(1)
}) })
// #2427
test('track length on for ... in iteration', () => {
const array = reactive([1])
let length = ''
effect(() => {
length = ''
for (const key in array) {
length += key
}
})
expect(length).toBe('0')
array.push(1)
expect(length).toBe('01')
})
describe('Array methods w/ refs', () => { describe('Array methods w/ refs', () => {
let original: any[] let original: any[]
beforeEach(() => { beforeEach(() => {

View File

@ -179,7 +179,7 @@ function has(target: object, key: string | symbol): boolean {
} }
function ownKeys(target: object): (string | number | symbol)[] { function ownKeys(target: object): (string | number | symbol)[] {
track(target, TrackOpTypes.ITERATE, ITERATE_KEY) track(target, TrackOpTypes.ITERATE, isArray(target) ? 'length' : ITERATE_KEY)
return Reflect.ownKeys(target) return Reflect.ownKeys(target)
} }