我想将Material Snackbar作为服务添加到我的应用中。
因此,在我的notification.service.ts中,我添加了:
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class NotificationService {
snackbar_notification: Subject<string> = new Subject();
constructor() {}
setSnackbarNotification(message: string) {
this.snackbar_notification.next(message);
}
}
在我的app.component.ts中:
export class AppComponent implements OnInit {
constructor(
private notificationService: NotificationService,
public snackBar: MatSnackBar
) {
this.notificationService.snackbar_notification.subscribe(message => {
this.snackBar.open(message);
});
}
}
我从类似以下的组件触发小吃栏
this.notificationService.setSnackbarNotification('this is a notification');
在我对上述3个文件进行上述更改之前,代码运行良好。
现在,它在执行过程中给了我错误:
Error: StaticInjectorError(Xs)[e -> e]:
StaticInjectorError(Platform: core)[e -> e]:
NullInjectorError: No provider for e!
我该如何解决?
答案 0 :(得分:2)
导入模块:
import {MatSnackBarModule} from '@angular/material/snack-bar';
并将其添加到您的模块中
@NgModule({
imports: [
MatSnackBarModule,
],
providers: [
],
})
确保要添加到通知的父模块及其中使用的组件。