我使用ngx-toastr库显示通知。此库包含ToastrService
。但是,我想为此服务创建自己的包装,因为我需要针对不同类型的消息使用不同的配置。所以我有:防止服务注入到其他服务
@Injectable()
export class NotificationService {
constructor(private toastrService: ToastrService) {
}
public success(message: string, title?: string): void {
this.toastrService.success(message, title);
}
public error(message: string, title?: string): void {
let toastConfig = {
...
};
this.toastrService.error(message, title, toastConfig);
}
public info(message: string, title?: string): void {
let toastConfig = {
...
};
this.toastrService.info(message, title, toastConfig);
}
public warning(message: string, title?: string): void {
this.toastrService.warning(message, title);
}
}
我想阻止其他开发人员在某处注入ToastrService。如果用户注入ToastrService到除NotificationService
以外的组件或其他服务,我想抛出错误。我怎样才能做到这一点?
模块:
@NgModule({
imports: [
ToastrModule.forRoot(),
],
declarations: [],
providers: [
NotificationService
],
exports: []
})
如何将它添加到您的应用程序? –
我更新了问题,如果我正确理解了你的话,我已经添加了模块定义。 – user348173