Angular 错误:找不到ErrorComponent的组件工厂。您是否将其添加到@NgModule.entryComponents?
我正在使用主应用程序中的以下代码摘要远程加载一些组件:Angular 错误:找不到ErrorComponent的组件工厂。您是否将其添加到@NgModule.entryComponents?,angular,angular-material2,systemjs,snackbar,Angular,Angular Material2,Systemjs,Snackbar,我正在使用主应用程序中的以下代码摘要远程加载一些组件: async loadRemoteModule() { const remoteModule = this.sysplexesService.getRemoteModule(); // These library are already present, so no need to reload them. SystemJS.set('@angular/core', SystemJS.newModule(Angula
async loadRemoteModule() {
const remoteModule = this.sysplexesService.getRemoteModule();
// These library are already present, so no need to reload them.
SystemJS.set('@angular/core', SystemJS.newModule(AngularCore));
SystemJS.set('@angular/common', SystemJS.newModule(AngularCommon));
SystemJS.set('@angular/material', SystemJS.newModule(AngularMaterial));
SystemJS.set('@angular/material/snack-bar', SystemJS.newModule(AngularMaterialSnackbar));
SystemJS.set('@angular/router', SystemJS.newModule(AngularRouter));
SystemJS.set('d3', SystemJS.newModule(await import('d3')));
SystemJS.set('@angular/common/http', SystemJS.newModule(HttpClientModule));
SystemJS.set('moment', SystemJS.newModule(await import('moment')));
SystemJS.set('@angular/flex-layout', SystemJS.newModule(FlexLayoutModule));
SystemJS.set('@angular/platform-browser/animations', SystemJS.newModule(NoopAnimationsModule));
SystemJS.set('@angular/forms', SystemJS.newModule(FormsModule));
SystemJS.set('rxjs', SystemJS.newModule(rxjs));
SystemJS.set('pi-common-lib', SystemJS.newModule(piCommonLib));
SystemJS.set('@angular/animations', SystemJS.newModule(AngularAnimation));
SystemJS.import(remoteModule.url).then((module) => {
this.compiler.compileModuleAndAllComponentsAsync(module[`${remoteModule.name}Module`])
.then((moduleFactory) => {
const moduleRef = moduleFactory.ngModuleFactory.create(this.injector);
let factory = moduleFactory.componentFactories.find(
item => item.componentType.name === `${remoteModule.name}Component`);
if (factory) {
const component = this.vc.createComponent(factory);
const instance = component.instance;
}
});
}
以下组件是由上一个例程加载的角度库的一部分:
其中一个库组件使用角度材质snackbar:
ssc.component.ts
notify(message: string, className: string) {
this.snackBar.openFromComponent(ErrorComponent, {
data: message, verticalPosition: 'top', panelClass: className,
// data: message, verticalPosition: 'top', duration: 20000, 3: className,
});
}
下面是自定义snackbar ErrorComponent:
import { Component, Inject } from '@angular/core';
import { MAT_SNACK_BAR_DATA, MatSnackBarRef } from '@angular/material/snack-bar';
@Component({
selector: 'izoa-error',
templateUrl: './error.component.html',
styleUrls: ['./error.component.scss'],
providers: [{ provide: MatSnackBarRef, useValue: {} }, {
provide: MAT_SNACK_BAR_DATA, useValue: {}
}]
})
export class ErrorComponent {
constructor(public snackBarRef: MatSnackBarRef<ErrorComponent>, @Inject(MAT_SNACK_BAR_DATA) public data: any) { }
}
当动态组件加载时,我得到以下错误:
core.js:15714 ERROR Error: No component factory found for ErrorComponent. Did you add it to @NgModule.entryComponents?
at noComponentFactoryError (core.js:9875)
at CodegenComponentFactoryResolver.resolveComponentFactory (core.js:9913)
at CdkPortalOutlet.attachComponentPortal (portal.es5.js:654)
at MatSnackBarContainer.attachComponentPortal (snack-bar.es5.js:392)
at MatSnackBar._attach (snack-bar.es5.js:856)
at MatSnackBar.openFromComponent (snack-bar.es5.js:695)
at SscComponent.notify (ssc.component.ts:330)
at SafeSubscriber.eval [as _next] (ssc.component.ts:200)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:196)
at SafeSubscriber.next (Subscriber.js:134)
errorComponent作为entryComponent存在
entryComponents: [ErrorComponent],
如果我改用基本的snackbar,那么就不需要/加载errorComponent
this.snackBar.open('We are sorry, but something went wrong. Please contact your administrator', 'Close', {
duration: 20000,
panelClass: className,
verticalPosition: 'top',
data: message
});
模块被动态加载,snackbar显示成功
如果我独立运行库,它可以与openFromComponent配合使用
this.snackBar.openFromComponent(ErrorComponent, {
data: message, verticalPosition: 'top', panelClass: className,
// data: message, verticalPosition: 'top', duration: 20000, 3: className,
});
只有当我尝试使用compiler.compileModuleAndAllComponentsAsync动态加载它时,我才会收到上一个错误
任何指针都将不胜感激。我还验证了“moduleFactory.ComponentFactorys”中存在ErrorComponent工厂。问题似乎源于角度材质snackbar无法正确注入工厂:this.snackbar.openFromComponent(ErrorComponent,我还验证了ErrorComponent工厂存在于'moduleFactory.ComponentFactorys'中。问题似乎源于角材料snackbar无法正确注入工厂:this.snackbar.openFromComponent(ErrorComponent,
this.snackBar.openFromComponent(ErrorComponent, {
data: message, verticalPosition: 'top', panelClass: className,
// data: message, verticalPosition: 'top', duration: 20000, 3: className,
});