File tree 3 files changed +18
-15
lines changed
3 files changed +18
-15
lines changed Original file line number Diff line number Diff line change 8
8
resolveDynamicComponent ,
9
9
h ,
10
10
serializeInner ,
11
- createVNode
11
+ createVNode ,
12
+ Comment ,
13
+ VNode
12
14
} from '@vue/runtime-test'
13
15
import { mockWarn } from '@vue/shared'
14
16
@@ -102,6 +104,7 @@ describe('resolveAssets', () => {
102
104
baz : { render : ( ) => 'baz' }
103
105
}
104
106
let foo , bar , baz // dynamic components
107
+ let dynamicVNode : VNode
105
108
106
109
const Child = {
107
110
render ( this : any ) {
@@ -115,6 +118,7 @@ describe('resolveAssets', () => {
115
118
return ( ) => {
116
119
foo = resolveDynamicComponent ( 'foo' ) // <component is="foo"/>
117
120
bar = resolveDynamicComponent ( dynamicComponents . bar ) // <component :is="bar"/>, function
121
+ dynamicVNode = createVNode ( resolveDynamicComponent ( null ) ) // <component :is="null"/>
118
122
return h ( Child , ( ) => {
119
123
// check inside child slots
120
124
baz = resolveDynamicComponent ( dynamicComponents . baz ) // <component :is="baz"/>, object
@@ -129,6 +133,8 @@ describe('resolveAssets', () => {
129
133
expect ( foo ) . toBe ( dynamicComponents . foo )
130
134
expect ( bar ) . toBe ( dynamicComponents . bar )
131
135
expect ( baz ) . toBe ( dynamicComponents . baz )
136
+ // should allow explicit falsy type to remove the component
137
+ expect ( dynamicVNode ! . type ) . toBe ( Comment )
132
138
} )
133
139
134
140
test ( 'resolve dynamic component should fallback to plain element without warning' , ( ) => {
Original file line number Diff line number Diff line change @@ -6,13 +6,7 @@ import {
6
6
ComponentOptions
7
7
} from '../component'
8
8
import { Directive } from '../directives'
9
- import {
10
- camelize ,
11
- capitalize ,
12
- isString ,
13
- isObject ,
14
- isFunction
15
- } from '@vue/shared'
9
+ import { camelize , capitalize , isString , isObject } from '@vue/shared'
16
10
import { warn } from '../warning'
17
11
18
12
const COMPONENTS = 'components'
@@ -22,14 +16,16 @@ export function resolveComponent(name: string): Component | string | undefined {
22
16
return resolveAsset ( COMPONENTS , name ) || name
23
17
}
24
18
19
+ export const NULL_DYNAMIC_COMPONENT = Symbol ( )
20
+
25
21
export function resolveDynamicComponent (
26
22
component : unknown
27
- ) : Component | string | undefined {
28
- if ( ! component ) return
23
+ ) : Component | string | typeof NULL_DYNAMIC_COMPONENT {
29
24
if ( isString ( component ) ) {
30
25
return resolveAsset ( COMPONENTS , component , false ) || component
31
- } else if ( isFunction ( component ) || isObject ( component ) ) {
32
- return component
26
+ } else {
27
+ // invalid types will fallthrough to createVNode and raise warning
28
+ return ( component as any ) || NULL_DYNAMIC_COMPONENT
33
29
}
34
30
}
35
31
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ import { currentScopeId } from './helpers/scopeId'
31
31
import { TeleportImpl , isTeleport } from './components/Teleport'
32
32
import { currentRenderingInstance } from './componentRenderUtils'
33
33
import { RendererNode , RendererElement } from './renderer'
34
+ import { NULL_DYNAMIC_COMPONENT } from './helpers/resolveAssets'
34
35
35
36
export const Fragment = ( Symbol ( __DEV__ ? 'Fragment' : undefined ) as any ) as {
36
37
__isFragment : true
@@ -254,15 +255,15 @@ export const createVNode = (__DEV__
254
255
: _createVNode ) as typeof _createVNode
255
256
256
257
function _createVNode (
257
- type : VNodeTypes | ClassComponent ,
258
+ type : VNodeTypes | ClassComponent | typeof NULL_DYNAMIC_COMPONENT ,
258
259
props : ( Data & VNodeProps ) | null = null ,
259
260
children : unknown = null ,
260
261
patchFlag : number = 0 ,
261
262
dynamicProps : string [ ] | null = null ,
262
263
isBlockNode = false
263
264
) : VNode {
264
- if ( ! type ) {
265
- if ( __DEV__ ) {
265
+ if ( ! type || type === NULL_DYNAMIC_COMPONENT ) {
266
+ if ( __DEV__ && ! type ) {
266
267
warn ( `Invalid vnode type when creating vnode: ${ type } .` )
267
268
}
268
269
type = Comment
You can’t perform that action at this time.
0 commit comments