Angular 6 EventEmitter不起作用

时间:2018-08-07 14:49:04

标签: angular angular6 eventemitter

这是服务     该服务实现了两个EventEmitter

 @Injectable()
    export class SpinnerService implements HttpInterceptor {
        visibility: EventEmitter<boolean> = new EventEmitter(false);
        notVisibility: EventEmitter<boolean> = new EventEmitter(false);
        constructor() { }
        intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
            **doesn 't work**
            this.visibility.emit(true);
            next.handle(request).subscribe(
                httpParameter => {
                    if (httpParameter instanceof HttpResponse) {
                        this.notVisibility.emit(false);
                    }
                });
            return next.handle(request);
        }
    }

这是组件

export class SpinnerComponent implements OnInit {
    visibility: boolean;
    ngOnInit() {
        this.subscribeToVisibility();
        this.subscribeToNotVisibility();
    }
    constructor(private spinnerService: SpinnerService) { }

    private subscribeToVisibility(): void {
        this.spinnerService.visibility.subscribe((value: boolean) => {
            **should enter here**
            this.visibility = value;
        });
    }
    private subscribeToNotVisibility(): void {
        this.spinnerService.notVisibility.subscribe((value: boolean) => {
            this.visibility = value;
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我认为将SpinnerService与拦截器分开并创建拦截器服务要好得多,通常在您的情况下,拦截器服务没有像您提到的那样调用,请检查我基于拦截器服务的Spinner实现。

stackblitz demo

拦截器服务

export class InterceptorService implements HttpInterceptor {

  constructor(private spinner: SpinnerService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.spinner.visibility.emit(true);
    return next.handle(req)
      .pipe(
      delay(1000), // delay is not required just to observe the effect
      finalize(() => this.spinner.notVisibility.emit(false))
      )
  }

}

旋转服务

export class SpinnerService {

  public visibility: EventEmitter<boolean> = new EventEmitter();
  public notVisibility: EventEmitter<boolean> = new EventEmitter();

}

将服务添加到AppModule提供程序数组

app.module

@NgModule({
  imports: [BrowserModule, HttpClientModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
  providers: [
     { provide: HTTP_INTERCEPTORS, useClass: InterceptorService, multi: true },
    , SpinnerService
  ]
})
export class AppModule { }

组件

export class AppComponent {

  public visibility: boolean = false;

  constructor(private _http: HttpClient, private spinnerService: SpinnerService) { }

  ngOnInit() {

    this.spinnerService.visibility.subscribe(state => {
      this.visibility = state;
      console.log('visibility', state);
    });

    this.spinnerService.notVisibility.subscribe(state => {
      this.visibility = state;
      console.log('notVisibility', state);
    });

  }

  getData() { // make http request 
    this._http.get('https://jsonplaceholder.typicode.com/todos/1')
      .subscribe(console.log)
  }
}

模板

<button (click)="getData()" [disabled]="visibility">Get Data!</button>

<div>
    visibility {{visibility}}
</div>