Angular 4 : http interceptor emit events to other components?

标签 angular event-handling

我有一个常用的 http 拦截器,它调用某个 API,如果它收到 403 响应,则必须发出一个事件。

拦截器:

@Injectable
export class CustomHttpInterceptor implements HttpInterceptor {
  @Output() notify: EventEmitter<boolean> = new EventEmitter<boolean>();

  constructor() {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.body.status === 403) {
      this.notify.emit(true);
    }
    return next.handle(req);
  }

}

然后我有一个 navi.component.html 会监听这个事件:

<md-toolbar (notify)="onNotify($event)">
  <a routerLink="/home">
      <img src="assets/images/logo.png" class="md-card-image p-navi-logo" alt="image caption"></a>
  <span class="spacer"></span>

  <div fxLayout="row" fxShow="false" fxShow.gt-sm>
    <development *ngIf="isLoggedIn"></development>
    <guide-menu></guide-menu>
    <documentation-menu></documentation-menu>
    <administration *ngIf="isAdmin"></administration>
    <about></about>
  </div>
...

top-navi.component.ts

public onNotify(result: boolean):void {
    console.log('notification received: ' + result);
    this.isLoggedIn = result;
  }

问题在于 top-navi-component 永远不会获取事件并且不会记录任何内容。我做错了什么?

最佳答案

按照以下步骤尝试这样发出值

创建新的服务文件或者如果你有现有的服务文件使用那个

sample.service.ts

@Injectable()
export class SampleService {
    notify: Subject<boolean> = new Subject<boolean>();
    onNotify(event) {
            this.notify.next(true);
    }
}

拦截器:

@Injectable()
export class CustomHttpInterceptor implements HttpInterceptor {

    constructor(private sampleService: SampleService) { }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        if (req.body.status === 403) {
            this.sampleService.onNotify(true)
        }
        return next.handle(req);
    }

}

navi.component.html

<md-toolbar>
    <a routerLink="/home">
        <img src="assets/images/logo.png" class="md-card-image p-navi-logo" alt="image caption">
    </a>
    <span class="spacer"></span>
    <div fxLayout="row" fxShow="false" fxShow.gt-sm>
        <development *ngIf="isLoggedIn"></development>
        <guide-menu></guide-menu>
        <documentation-menu></documentation-menu>
        <administration *ngIf="isAdmin"></administration>
        <about></about>
    </div>
</md-toolbar>

最后 top-navi.component.ts

export class TopNaviComponent {

    constructor(private sampleService: SampleService) {

        this.sampleService.notify.subscribe((result) => {
            console.log('result', result)
        })

    }
}

关于 Angular 4 : http interceptor emit events to other components?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47714984/

相关文章:

angular - 如何在 Electron 应用程序外部复制和粘贴?

jquery 数据表 : fetch next 25 rows from databse on next page button of jQuery datatable

c++ - 如何正确处理鼠标事件处理程序中的不同情况?

javascript - 当使用 JavaScript 单击方 block 时,如何使方 block 的颜色发生变化?

C# 如何创建一个发布事件和订阅类的单例?

c# - WPF 单击事件处理程序获取文本 block 文本

javascript - Angular 2 : Bind view with callback variable

typescript - 将每个数组元素绑定(bind)到选项标签

android - 如何在 ACTION_MOVE 过程中用另一根手指检测 TOUCH 事件

javascript - Angular - 无法获取父组件数据