我收到一个
TypeError:无法读取未定义的属性“createComponent”
在尝试动态加载组件时。
import {
AfterViewInit,
ViewChild,
ViewContainerRef,
Component,
ComponentFactory,
ComponentFactoryResolver,
ComponentRef,
OnDestroy,
} from '@angular/core';
import { AddFabComponent } from '@app/shared/components/fabs/add-fab/add-fab.component'
@Component({
selector: 'app-airframe-list',
templateUrl: './airframe-list.component.html',
styleUrls: ['./airframe-list.component.scss']
})
export class AirframeListComponent implements AfterViewInit, OnDestroy {
componentRef: any;
destroy: any;
@ViewChild('fabconainter', { read: ViewContainerRef }) fabconainter: ViewContainerRef;
constructor(private resolver: ComponentFactoryResolver) { }
ngAfterViewInit() {
this.createComponent();
}
ngOnDestroy() {
this.componentRef.destroy();
}
createComponent() {
const factory = this.resolver.resolveComponentFactory(AddFabComponent);
const componentRef = this.fabconainter.createComponent(factory);
}
}
我也尝试过不同的容器(以防万一)
<div #fabconainter></div>
<ng-template #fabconainter></ng-template>
<template #fabconainter></template>
我迷路了。任何代码示例也将不胜感激。