Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): get refs when using for directives. #5449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
Expand Up @@ -442,4 +442,43 @@ describe('api: template refs', () => {
await nextTick()
expect(mapRefs()).toMatchObject(['2', '3', '4'])
})

// #5447
test('string ref in v-for', async () => {
const list = reactive([1, 2, 3])
const listRefs = ref([])
const mapRefs = () => listRefs.value.map(n => serializeInner(n))
const App = {
setup() {
return { listRefs }
},
render() {
return h(
'ul',
list.map(i =>
h(
'li',
{
ref: 'listRefs',
ref_for: true
},
i
)
)
)
}
}
const root = nodeOps.createElement('div')
render(h(App), root)

expect(mapRefs()).toMatchObject(['1', '2', '3'])

list.push(4)
await nextTick()
expect(mapRefs()).toMatchObject(['1', '2', '3', '4'])

list.shift()
await nextTick()
expect(mapRefs()).toMatchObject(['2', '3', '4'])
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/rendererTemplateRef.ts
Expand Up @@ -84,7 +84,7 @@ export function setRef(
if (_isString || _isRef) {
const doSet = () => {
if (rawRef.f) {
const existing = _isString ? refs[ref] : ref.value
const existing = _isString ? refs[ref] || setupState[ref] : ref.value
if (isUnmount) {
isArray(existing) && remove(existing, refValue)
} else {
Expand Down