refactor: simplify resolveDyanmicComponent

This commit is contained in:
Evan You 2020-03-16 12:46:15 -04:00
parent 9ad65b1653
commit 19228a469a
3 changed files with 8 additions and 16 deletions

View File

@ -814,8 +814,7 @@ describe('compiler: element transform', () => {
{ {
type: NodeTypes.SIMPLE_EXPRESSION, type: NodeTypes.SIMPLE_EXPRESSION,
content: 'foo' content: 'foo'
}, }
'$'
] ]
} }
}) })

View File

@ -215,11 +215,9 @@ export function resolveComponentType(
} }
// dynamic <component :is="asdf" /> // dynamic <component :is="asdf" />
else if (isProp.exp) { else if (isProp.exp) {
return createCallExpression( return createCallExpression(context.helper(RESOLVE_DYNAMIC_COMPONENT), [
context.helper(RESOLVE_DYNAMIC_COMPONENT), isProp.exp
// _ctx.$ exposes the owner instance of current render function ])
[isProp.exp, context.prefixIdentifiers ? `_ctx.$` : `$`]
)
} }
} }

View File

@ -23,16 +23,11 @@ export function resolveComponent(name: string): Component | undefined {
} }
export function resolveDynamicComponent( export function resolveDynamicComponent(
component: unknown, component: unknown
// Dynamic component resolution has to be called inline due to potential
// access to scope variables. When called inside slots it will be inside
// a different component's render cycle, so the owner instance must be passed
// in explicitly.
instance: ComponentInternalInstance
): Component | undefined { ): Component | undefined {
if (!component) return if (!component) return
if (isString(component)) { if (isString(component)) {
return resolveAsset(COMPONENTS, component, instance) return resolveAsset(COMPONENTS, component, currentRenderingInstance)
} else if (isFunction(component) || isObject(component)) { } else if (isFunction(component) || isObject(component)) {
return component return component
} }
@ -46,13 +41,13 @@ export function resolveDirective(name: string): Directive | undefined {
function resolveAsset( function resolveAsset(
type: typeof COMPONENTS, type: typeof COMPONENTS,
name: string, name: string,
instance?: ComponentInternalInstance instance?: ComponentInternalInstance | null
): Component | undefined ): Component | undefined
// overload 2: directives // overload 2: directives
function resolveAsset( function resolveAsset(
type: typeof DIRECTIVES, type: typeof DIRECTIVES,
name: string, name: string,
instance?: ComponentInternalInstance instance?: ComponentInternalInstance | null
): Directive | undefined ): Directive | undefined
function resolveAsset( function resolveAsset(