我有一个共享模块,其中包含常见组件,例如标题和通知组件。我有另一个名为 ProductModule 的模块,它使用共享模块中的通知组件并调用通知组件中定义的函数。
shared.module.ts
@NgModule({
declarations: [
HeaderComponent,
SideNavComponent,
SpinnerComponent,
ItemSummaryComponent,
UserRoleDirective,
EmptyResultComponent,
NotificationComponent
],
imports: [
CommonModule,
RouterModule,
MatProgressSpinnerModule,
MatIconModule
],
exports: [
HeaderComponent,
SideNavComponent,
SpinnerComponent,
ItemSummaryComponent,
EmptyResultComponent,
NotificationComponent
],
})
export class SharedModule {}
通知组件.ts
export class NotificationComponent {
openSnackBar(message: string, state: string, icon: string): void {
const config = new MatSnackBarConfig();
config.duration = 3000;
config.panelClass = ['nc-notification', state];
config.data = { message, icon };
this._snackBar.openFromComponent(NotificationComponent, config);
}
}
在我的延迟加载productModuel
中,我已经导入了我sharedModule
的如下。
import { CommonCmpsModule } from '../common-cmps/common-cmps.module';
import { TaxConfigurationComponent } from './tax-configuration/tax-configuration.component';
@NgModule({
declarations: [
TaxConfigurationComponent
],
imports: [
SharedModule,
]
})
export class ProductModule { }
在我的TaxConfigurationComponent
中,我想使用通知组件并调用 openSnackBar 函数。
TaxConfiguration.component.ts
import { NotificationComponent } from 'src/app/common-cmps/notification/notification.component';
export class TaxConfigurationComponent {
constructor(
private notificationService: NotificationService
){}
onClickSave(): void {
this.notificationService.openSnackBar('An Error Occurred', 'nc-notification--error', 'close');
}
}
但是,我在浏览器控制台中遇到了错误。
错误错误:未捕获(承诺):NullInjectorError:R3InjectorError(ProductManagementModule)[NotificationComponent -> NotificationComponent -> NotificationComponent -> NotificationComponent]:NullInjectorError:NotificationComponent 没有提供者!