下面的代码在 Angular 13 中动态创建组件
指令:
import { Directive, Input, ViewContainerRef, Type } from '@angular/core';
@Directive({
selector: '[appLoader]'
})
export class LoaderDirective {
@Input() appLoader!: Type<any>;
constructor(private viewContainerRef: ViewContainerRef) {}
ngOnInit(): void {
this.viewContainerRef.createComponent(this.appLoader);
}
}
app.component.html
<div [appLoader]="component"></div>
app.component.ts
component = MyComponent
我还需要将一个对象传递给创建的组件,这是一些将由创建的组件使用的额外信息。
我怎样才能做到这一点?