fix: dynamic component fallback to native element

fix #870
This commit is contained in:
Evan You
2020-03-23 14:47:04 -04:00
parent 1b2149dbb2
commit f529dbde23
7 changed files with 80 additions and 34 deletions

View File

@@ -798,9 +798,18 @@ describe('compiler: element transform', () => {
describe('dynamic component', () => {
test('static binding', () => {
const { node, root } = parseWithBind(`<component is="foo" />`)
expect(root.helpers).not.toContain(RESOLVE_DYNAMIC_COMPONENT)
expect(root.helpers).toContain(RESOLVE_DYNAMIC_COMPONENT)
expect(node).toMatchObject({
tag: '_component_foo'
tag: {
callee: RESOLVE_DYNAMIC_COMPONENT,
arguments: [
{
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'foo',
isStatic: true
}
]
}
})
})
@@ -813,7 +822,8 @@ describe('compiler: element transform', () => {
arguments: [
{
type: NodeTypes.SIMPLE_EXPRESSION,
content: 'foo'
content: 'foo',
isStatic: false
}
]
}

View File

@@ -204,19 +204,13 @@ export function resolveComponentType(
// 1. dynamic component
const isProp = node.tag === 'component' && findProp(node, 'is')
if (isProp) {
// static <component is="foo" />
if (isProp.type === NodeTypes.ATTRIBUTE) {
const isType = isProp.value && isProp.value.content
if (isType) {
context.helper(RESOLVE_COMPONENT)
context.components.add(isType)
return toValidAssetId(isType, `component`)
}
}
// dynamic <component :is="asdf" />
else if (isProp.exp) {
const exp =
isProp.type === NodeTypes.ATTRIBUTE
? isProp.value && createSimpleExpression(isProp.value.content, true)
: isProp.exp
if (exp) {
return createCallExpression(context.helper(RESOLVE_DYNAMIC_COMPONENT), [
isProp.exp
exp
])
}
}