fix(compiler-core): should attach key to single element child of <template v-for> (#1910)

This commit is contained in:
underfin 2020-08-20 22:09:57 +08:00 committed by GitHub
parent 7ffb79c563
commit 69cfed6b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

View File

@ -104,6 +104,23 @@ return function render(_ctx, _cache) {
}"
`;
exports[`compiler: v-for codegen template v-for key injection with single child 1`] = `
"const _Vue = Vue
return function render(_ctx, _cache) {
with (_ctx) {
const { renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock, createVNode: _createVNode } = _Vue
return (_openBlock(true), _createBlock(_Fragment, null, _renderList(items, (item) => {
return (_openBlock(), _createBlock(\\"span\\", {
key: item.id,
id: item.id
}, null, 8 /* PROPS */, [\\"id\\"]))
}), 128 /* KEYED_FRAGMENT */))
}
}"
`;
exports[`compiler: v-for codegen template v-for w/ <slot/> 1`] = `
"const _Vue = Vue

View File

@ -770,6 +770,29 @@ describe('compiler: v-for', () => {
expect(generate(root).code).toMatchSnapshot()
})
// #1907
test('template v-for key injection with single child', () => {
const {
root,
node: { codegenNode }
} = parseWithForTransform(
'<template v-for="item in items" :key="item.id"><span :id="item.id" /></template>'
)
expect(assertSharedCodegen(codegenNode, true)).toMatchObject({
source: { content: `items` },
params: [{ content: `item` }],
innerVNodeCall: {
type: NodeTypes.VNODE_CALL,
tag: `"span"`,
props: createObjectMatcher({
key: '[item.id]',
id: '[item.id]'
})
}
})
expect(generate(root).code).toMatchSnapshot()
})
test('v-for on <slot/>', () => {
const {
root,

View File

@ -145,6 +145,9 @@ export const transformFor = createStructuralDirectiveTransform(
// but mark it as a block.
childBlock = (children[0] as PlainElementNode)
.codegenNode as VNodeCall
if (isTemplate && keyProperty) {
injectProp(childBlock, keyProperty, context)
}
childBlock.isBlock = !isStableFragment
if (childBlock.isBlock) {
helper(OPEN_BLOCK)