Angular 如何在另一个服务中使用AlertService(mat snackbar用于错误消息)?

Angular 如何在另一个服务中使用AlertService(mat snackbar用于错误消息)?,angular,typescript,angular-material,Angular,Typescript,Angular Material,我正在尝试订阅一个服务中的可观察到的。但是,我需要使用AlertService来显示错误。另一个服务中的服务(循环依赖?) 这是警报服务 @Injectable() 导出类警报服务{ 私有主体=新主体(); private keepAfterNavigationChange=false; 构造函数(专用路由器:路由器){ //清除有关路线更改的警报消息 router.events.subscribe(事件=>{ if(导航启动的事件实例){ if(this.keepAfterNavigatio

我正在尝试订阅一个服务中的可观察到的。但是,我需要使用
AlertService
来显示错误。另一个服务中的服务(循环依赖?)

这是警报服务

@Injectable()
导出类警报服务{
私有主体=新主体();
private keepAfterNavigationChange=false;
构造函数(专用路由器:路由器){
//清除有关路线更改的警报消息
router.events.subscribe(事件=>{
if(导航启动的事件实例){
if(this.keepAfterNavigationChange){
//仅保留一次位置更改
this.keepAfterNavigationChange=false;
}否则{
//明确警报
this.subject.next();
}
}
});
}
成功(消息:string,keepAfterNavigationChange=false){
this.keepAfterNavigationChange=keepAfterNavigationChange;
this.subject.next({type:'success',text:message});
}
错误(消息:string,keepAfterNavigationChange=false){
this.keepAfterNavigationChange=keepAfterNavigationChange;
this.subject.next({type:'error',text:message});
}
getMessage():可观察{
返回此.subject.asObservable();
}

}
您的服务可以注入到其他服务中

@Injectable()export class EmailService {

constructor(private http: HttpClient, private alertService: AlertService) { }

sendEmail(email: Email) {
    return this.http.post(BACKEND_URL + 'send', email).map( result => this.alertService.alert(result););


  }
}

如果AlertService使用EmailService,而EmailService使用AlertService,则可能是循环的

在服务中使用服务会出现什么问题?这不是循环依赖关系我尝试在EmailComponent中使用AlertComponent,但我得到了一个循环依赖关系控制台AlertConsole,这意味着您具有循环依赖关系,但这不是因为在服务中使用服务