Angular 9 - No component factory found for <component>

Viewed 2720

After updating an Angular app from version 8 to 9, I'm getting the following error: No component factory found for <component>. Did you add it to @NgModule.entryComponents?

But as stated on Angular Update Guide, adding components to entryComponents should not be required anymore:

With Angular 9 Ivy is now the default rendering engine

...

If you have specified any entryComponents in your NgModules or had any uses of ANALYZE_FOR_ENTRY_COMPONENTS, you can remove them. They are no longer required with the Ivy compiler and runtime.

I'm using the following package versions:

"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",
}

This is the code I'm using to dynamically create components:

@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);
}
1 Answers

It was kind of my bad, but I'm answering it here as it seems other people are also having the same issue.

Although the entire project was updated to Angular 9, Ivy was disabled under tsconfig.json (I actually did some tests with Ivy on Angular 8 and didn't realize it was disabled):

"angularCompilerOptions": {
  "enableIvy": false
}

I've enabled it by removing enableIvy setting and the issue is solved.

Related