( viewContainer: ViewContainerRef, options: RenderSlotOptions<T>, )
| 39 | * ``` |
| 40 | */ |
| 41 | export function renderSlot<T = any>( |
| 42 | viewContainer: ViewContainerRef, |
| 43 | options: RenderSlotOptions<T>, |
| 44 | ): ComponentRef<T> | EmbeddedViewRef<T> | null { |
| 45 | const { slot, defaultComponent, props, injector, outputs } = options; |
| 46 | |
| 47 | viewContainer.clear(); |
| 48 | |
| 49 | const effectiveSlot = slot ?? defaultComponent; |
| 50 | const effectiveInjector = injector ?? viewContainer.injector; |
| 51 | |
| 52 | if (effectiveSlot instanceof TemplateRef) { |
| 53 | // TemplateRef: render template |
| 54 | return viewContainer.createEmbeddedView(effectiveSlot, { |
| 55 | $implicit: props ?? {}, |
| 56 | props: props ?? {}, |
| 57 | } as any); |
| 58 | } else if (isComponentType(effectiveSlot)) { |
| 59 | // Component type - wrap in try/catch for safety |
| 60 | try { |
| 61 | return createComponent( |
| 62 | viewContainer, |
| 63 | effectiveSlot as Type<T>, |
| 64 | props, |
| 65 | effectiveInjector, |
| 66 | outputs, |
| 67 | ); |
| 68 | } catch (error) { |
| 69 | console.warn("Failed to create component:", effectiveSlot, error); |
| 70 | // Fall through to default component |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Default: render default component if provided |
| 75 | return defaultComponent |
| 76 | ? createComponent( |
| 77 | viewContainer, |
| 78 | defaultComponent, |
| 79 | props, |
| 80 | effectiveInjector, |
| 81 | outputs, |
| 82 | ) |
| 83 | : null; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Creates a component and applies properties. |
no test coverage detected
searching dependent graphs…