【发布时间】:2021-09-03 03:32:20
【问题描述】:
我正在开发一个应用程序,目前,我有一个动态网格生成器,它可以划分屏幕空间以动态适应多个组件。因此,它的组件必须在 Angular 渲染页面后渲染组件。为了实现这一点,我遵循了角度动态组件加载器指南 (https://angular.io/guide/dynamic-component-loader)。
所以我现在确实拥有必须渲染其他组件的组件,我有我的自定义指令来渲染组件。
指令
@Directive({
selector: '[componentLoader]'
})
export class ComponentLoaderDirective {
constructor (
public ViewContainerRef: ViewContainerRef
) {}
}
现在的组件(网格组件) grid.component.ts
// ... Stuff above
export class GridComponent implements OnInit {
@Input() public items: gridItem[] = [];
@ViewChild(ComponentLoaderDirective) componentLoader: ComponentLoaderDirective | undefined;
constructor(
private sanitizer: DomSanitizer,
private componentFactoryResolver: ComponentFactoryResolver
) {}
ngOnInit(): void { this.processRow(this.items) }
processRow( row: gridItem[] ) {
// Some grid related stuff ...
for ( let item of row ) {
// Stuff performed over every item in each grid row
this.renderComponentIfNeeded(item)
}
}
renderComponentIfNeeded( item: gridItem ):void {
if ( item.components ) {
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(component);
let viewContainerRef = this.componentLoader.ViewContainerRef;
viewContainerRef.clear();
let componentRef = viewContainerRef.createComponent<any>(componentFactory);
componentRef.instance.data = item;
console.log('Directive ', this.componentLoader, 'ComponentRef: ', componentRef);
}
}
以及组件的HTML:
<!-- Dynamic grid generation using ng-template and ng-content. This is generated several times using the *ngFor, for every item in the items array we will have a componentLoader -->
<ng-template componentLoader>
</ng-template>
这些文件中有更多内容,但为简单起见,我只发布此内容,如果您需要更多代码,请告诉我。
好的,所以我的问题是当我访问this.contentLoader 时返回的值是未定义的,所以this.componentLoader.viewContainerRef 会导致错误,因为componentLoader 是未定义的。
我尝试将 exportAs 属性添加到指令的装饰器中,但它给出的错误完全相同。
我也尝试在模块声明中添加指令但没有成功,并将 <ng-template componentLoader> 更改为 <ng-template #loader=componentLoader> 导致不同的错误(没有指令有 'componentLoader' exportAs 或类似的东西)
PS:在 ´´´this.componentFacotryResolver.resolveComponentFactory(component)``` 中,我成功地拥有了已分配给网格的每个组件。
我希望您不要解决我的问题,而是为我指明正确的方向并帮助我了解我做错了什么以提高自己。
任何帮助将不胜感激:)
【问题讨论】:
标签: javascript angular dynamic components directive