После обновления приложения Angular с версии 8 до 9 я получаю следующую ошибку: No component factory found for <component>. Did you add it to @NgModule.entryComponents?
Но, как указано в Angular Руководство по обновлению , добавление компоненты для entryComponents
больше не требуются:
С Angular 9 Ivy теперь является механизмом рендеринга по умолчанию
...
Если вы указали какие-либо entryComponents в ваших NgModules или использовали ANALYZE_FOR_ENTRY_COMPONENTS, вы можете удалить их. Они больше не требуются для компилятора Ivy и среды выполнения.
Я использую следующие версии пакетов:
"dependencies": {
"@angular/animations": "~9.0.3",
"@angular/cdk": "~9.1.0",
"@angular/common": "~9.0.3",
"@angular/compiler": "~9.0.3",
"@angular/core": "~9.0.3",
"@angular/forms": "~9.0.3",
"@angular/material": "^9.1.0",
"@angular/platform-browser": "~9.0.3",
"@angular/platform-browser-dynamic": "~9.0.3",
"@angular/router": "~9.0.3",
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.900.3",
"@angular-devkit/build-ng-packagr": "~0.900.3",
"@angular/cli": "~9.0.3",
"@angular/compiler-cli": "~9.0.3",
"@angular/language-service": "~9.0.3",
}
Это код, который я использую для динамического создания компоненты:
@ViewChild('target', {static: true, read: ViewContainerRef}) target: ViewContainerRef;
@Input() componentType: Type<Component>;
cmpRef: ComponentRef<Component>;
constructor(private componentFactoryResolver: ComponentFactoryResolver) {}
ngOnInit() {
this.updateComponent();
}
ngOnChanges() {
this.updateComponent();
}
ngOnDestroy() {
if (this.cmpRef) {
this.cmpRef.destroy();
}
}
updateComponent() {
if (this.cmpRef) {
this.cmpRef.destroy();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(this.componentType);
this.cmpRef = this.target.createComponent(factory);
}