emittieren Ich versuche, ein Ereignis von HttpInterceptor zu senden.Wie ein Ereignis von HttpInterceptor im Winkel 5
-Code meiner Veranstaltungen Service:
import { EventEmitter } from '@angular/core';
export class Message {
constructor(public name:string, public data:any){
}
}
export class NotifyService {
dispatcher: EventEmitter<any> = new EventEmitter();
constructor() {}
emitMessageEvent(name:string, data:any){
let message = new Message(name,data);
this.dispatcher.emit(message);
}
getEmitter() {
return this.dispatcher;
}
Eine Komponente, die Ereignisse
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { NotifyService } from './services/notify.service';
@Component({
...
providers: [ NotifyService ]
})
export class AppComponent {
subscription: any;
constructor(private router: Router, private notifyService: NotifyService) {
}
ngOnInit() {
this.subscription = this.notifyService.getEmitter()
.subscribe(item => this.showAppLevelAlarm(item));
}
private showAppLevelAlarm(_notify: any): void {
// this.message = _notify.msg;
console.log(_notify);
}
}
Und mein HttpInterceptor erhält:
import { NotifyService } from '../services/notify.service';
@Injectable()
export class ApiInterceptor implements HttpInterceptor {
constructor(public notifyService: NotifyService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).do(event => {
return event;
},
error => {
this.notifyService.emitMessageEvent('showAppLevelAlert', error.error);
})
Helfen Sie mir zu verstehen, warum die Ereignisse, die aus gesendet wurden Der HttpInterceptor wird von der AppComponent nicht empfangen. Wenn ich jedoch ein Ereignis von einer anderen Komponente sende, empfängt die AppComponent ein Ereignis.