如何将HTTP请求中的布尔值发送到组件?(角度) [英] How to send a boolean from a http request to a component?(Angular)
问题描述
我用Angular 7创建了一个程序,我想以boolean捕获http请求并将其传输到组件.我试图将BehaviourSubject与next一起使用.因此,在这两个函数checkin()和checkOut()中,布尔值必须为true.我附上了代码.我将不胜感激任何帮助.谢谢!
I created a program with Angular 7 and i want to catch the http request in a boolean and to transfer it to a component. I tried to use BehaviourSubject with next for this. So the boolean has to be true inside these two functions checkin() and checkOut(). I attached the code. I would appreciate any help. Thanks!
import { Component, OnInit } from '@angular/core';
import { HttpLoadingInterceptor } from '../../../interceptor/http-loading.interceptor';
import { Subscription } from 'rxjs';
@Component({
selector: 'app',
templateUrl: './app.html',
styleUrls: ['./app.scss']
})
export class App implements OnInit {
subscriptionBlockUI: Subscription;
blockUIState: boolean;
constructor(
private httpLoadingInterceptor: HttpLoadingInterceptor
) { }
ngOnInit() {
this.subscriptionBlockUI = this.httpLoadingInterceptor.blockUIStateValue().subscribe((blockUIValue: boolean) => {
this.blockUIState = blockUIValue;
});
}
ngOnDestroy(): void {
this.subscriptionBlockUI.unsubscribe();
}
}
import { Injectable } from '@angular/core';
import { BlockUIService } from 'ng-block-ui';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable, BehaviorSubject } from 'rxjs';
import { finalize } from 'rxjs/operators';
import { CompileShallowModuleMetadata } from '@angular/compiler';
export const HEADER_LOADING = 'loadingEnabled';
@Injectable()
export class HttpLoadingInterceptor implements HttpInterceptor {
readonly blockUiSelector: string = 'http-request';
private count = 0;
public blockUIState = new BehaviorSubject<boolean>(false);
constructor(private uiServ: BlockUIService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.headers.has(HEADER_LOADING)) {
return next.handle(req);
}
this.checkin();
return next.handle(req).pipe(finalize(() => this.checkOut()));
}
public checkin() {
if (this.count === 0) {
this.uiServ.start([this.blockUiSelector]);
}
this.count++;
this.blockUIState.next(true);
}
public checkOut() {
this.count--;
setTimeout(() => {
if (this.count === 0) {
this.uiServ.stop([this.blockUiSelector]);
}
}, 10);
this.blockUIState.next(true);
}
public blockUIStateValue(): Observable<boolean> {
return this.blockUIState;
}
}
推荐答案
好吧,如果您没有获得该值,则可能是希望通过BehaviorSubject将blockUIState
设为可观察对象.
Okay, if you don't get the value, may be you would want to make blockUIState
an Observable via a BehaviorSubject.
在service.ts
In service.ts
public blockUIState = new BehaviorSubject<boolean>(false);
blockUIState$ = this.blockUIState.asObservable();
在component.ts
In component.ts
ngOnInit() {
this.subscriptionBlockUI = this.httpLoadingInterceptor.blockUIState$.subscribe((blockUIValue: boolean) => {
this.blockUIState = blockUIValue;
});
}
以前的更正不起作用. 尝试设置调试器</p>
previous correction is not working. Try to set a debugger
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.headers.has(HEADER_LOADING)) {
return next.handle(req);
}
debugger;
this.checkin();
return next.handle(req).pipe(finalize(() => this.checkOut()));
}
因为checkin()仅在本节中被调用.请尝试.
Because checkin() is only called in this section. please try it.
请从if子句中删除next.handle(req)
.该代码现在应该是:
please remove the next.handle(req)
from if clause. The code should be for now:
if (req.headers.has(HEADER_LOADING)) {
// return next.handle(req);
}
this.checkin();
return next.handle(req).pipe(finalize(() => this.checkOut()));
因为,当条件为true时,它将返回并且不会调用`this.checkin()中的下一部分.
Because, when the condition is true, it will return and the next portion from `this.checkin() is not called.
这篇关于如何将HTTP请求中的布尔值发送到组件?(角度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!