Как вы можете динамически создать компонент в ngOnInit ()?
При создании компонента в ngOnInit я получаю сообщение об ошибке "Не удается прочитать свойство 'clear' of undefined".
Вот мой компонент:
import { Component, OnInit, ViewChild, ViewContainerRef, ComponentFactoryResolver, ComponentRef, ComponentFactory } from '@angular/core';
import { SpinnerService } from '../../tools/spinner/spinner.service';
import { CardService } from './card.service';
import { CardDatagridComponent } from './card-datagrid/card-datagrid.component';
@Component({
selector: 'app-card',
templateUrl: './card.component.html',
styleUrls: ['./card.component.scss']
})
export class CardComponent implements OnInit {
@ViewChild("cardDataGridContainer", { read: ViewContainerRef }) container;
public renderReady: boolean = false;
public componentRef: ComponentRef<any>;
public selectedStatus = 'A';
constructor(private spinner: SpinnerService, private cardService: CardService, private resolver: ComponentFactoryResolver) { }
ngOnInit() {
this.spinner.show();
setTimeout(() => {
this.spinner.hide();
this.renderReady = true;
}, 2000);
this.selectTabStatus(this.selectedStatus);
}
selectTabStatus(status) {
this.selectedStatus = status;
this.createComponent(status);
}
createComponent(status) {
this.container.clear();
const factory: ComponentFactory<any> = this.resolver.resolveComponentFactory(CardDatagridComponent)
this.componentRef = this.container.createComponent(factory);
this.componentRef.instance.cardStatus = status;
}
ngOnDestroy() {
this.componentRef.destroy();
}
}
Любое предложение, ребята?Спасибо!