fix(runtime-core): fix v-for ref reactivity behavior difference between prod and dev (#6714)

fix #6697
This commit is contained in:
白雾三语 2022-10-14 17:23:01 +08:00 committed by GitHub
parent 82a73da351
commit 9ae796d156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 1 deletions

View File

@ -493,4 +493,50 @@ describe('api: template refs', () => {
await nextTick() await nextTick()
expect(mapRefs()).toMatchObject(['2', '3', '4']) expect(mapRefs()).toMatchObject(['2', '3', '4'])
}) })
// #6697 v-for ref behaves differently under production and development
test('named ref in v-for , should be responsive when rendering', async () => {
const list = ref([1, 2, 3])
const listRefs = ref([])
const App = {
setup() {
return { listRefs }
},
render() {
return h('div', null, [
h('div', null, String(listRefs.value)),
h(
'ul',
list.value.map(i =>
h(
'li',
{
ref: 'listRefs',
ref_for: true
},
i
)
)
)
])
}
}
const root = nodeOps.createElement('div')
render(h(App), root)
await nextTick()
expect(String(listRefs.value)).toBe(
'[object Object],[object Object],[object Object]'
)
expect(serializeInner(root)).toBe(
'<div><div>[object Object],[object Object],[object Object]</div><ul><li>1</li><li>2</li><li>3</li></ul></div>'
)
list.value.splice(0, 1)
await nextTick()
expect(String(listRefs.value)).toBe('[object Object],[object Object]')
expect(serializeInner(root)).toBe(
'<div><div>[object Object],[object Object]</div><ul><li>2</li><li>3</li></ul></div>'
)
})
}) })

View File

@ -84,7 +84,11 @@ export function setRef(
if (_isString || _isRef) { if (_isString || _isRef) {
const doSet = () => { const doSet = () => {
if (rawRef.f) { if (rawRef.f) {
const existing = _isString ? refs[ref] : ref.value const existing = _isString
? hasOwn(setupState, ref)
? setupState[ref]
: refs[ref]
: ref.value
if (isUnmount) { if (isUnmount) {
isArray(existing) && remove(existing, refValue) isArray(existing) && remove(existing, refValue)
} else { } else {