代码之家  ›  专栏  ›  技术社区  ›  Muhammed Ozdogan

角度6主题订阅不工作

  •  1
  • Muhammed Ozdogan  · 技术社区  · 6 年前

    http interceptor 它在请求开始和结束时发出“字符串”:

    @Injectable({
      providedIn: 'root'
    })
    export class LoadingIndicatorService implements HttpInterceptor {
    
      private loadingIndicatorSource = new  Subject<string>();
      private loadingIndicator$ = this.loadingIndicatorSource.asObservable();
    
      intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        this.updateVisibility('block');
        return next.handle(req)
          .pipe(
            finalize(
              () => {
                this.updateVisibility('none');
              }
            )
          )
          ;
      }
    
      updateVisibility(state: string) {
        this.loadingIndicatorSource.next(state);
      }
    
      getLoadingIndicator() {
        return this.loadingIndicator$;
      }
    }
    

    export class AppComponent {
    
    
      display = 'none';
    
      constructor(public  authenticationService: AuthenticationService,
                  public loadingIndicatorService: LoadingIndicatorService) {
        this.loadingIndicatorService.getLoadingIndicator().subscribe(visibility => {
          console.log(visibility);
          this.display = visibility;
        });
    
        }
      }
    

    实际上,我想显示一个装载指示器:

     <div [style.display]="display">
        <mat-progress-bar mode="indeterminate" color="warn" ></mat-progress-bar>
      </div>
    

    我流动 this

    但是subscribe方法永远不会执行。

    为什么订阅方法不起作用?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Nico    6 年前

    我要做的是创建一个SpinnerService和一个spinnerchiptor,而不是将它们合并在一起。

    使微调器服务分配未完成请求的数量,并在未完成请求大于0时显示微调器。

    @Injectable()
    export class SpinnerService {
        private requestAmount$ = new BehaviorSubject(0);
    
    public showSpinner$ = this.requestAmount$.asObservable().pipe(map(r => r > 0));
    
    requestStart() {
        this.requestAmount$.next(this.requestAmount$.getValue() + 1);
    }
    
    requestEnd() {
        this.requestAmount$.next(this.requestAmount$.getValue() - 1);
    }
    }
    

    @Injectable()
    export class SpinnerInterceptor implements HttpInterceptor {
        constructor(private spinnerService: SpinnerService) {}
    
    intercept(req: HttpRequest<any>, next: HttpHandler) {
            this.spinnerService.requestStart();
    
            return next.handle(req).pipe(
                finalize(() => {
                    this.spinnerService.requestEnd();
                })
            );
        }
    }
    

    在应用程序组件中使用 ngOnInit 钩子不使用它与样式指南相反的构造函数。

    export class AppComponent implements OnInit{
    
    
      display = 'none';
    
      constructor(public  authenticationService: AuthenticationService,
                  public spinnerService: SpinnerService) {}
    ngOnInit(){
        this.spinnerService.showSpinner$.subscribe(visibility => {
          console.log(visibility);
          this.display = visibility ? 'block': 'none';
        });
    
        }
      }
    

    希望这有帮助!