Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以在Angular 8中定义函数前置/后置处理器?_Angular_Refactoring - Fatal编程技术网

是否可以在Angular 8中定义函数前置/后置处理器?

是否可以在Angular 8中定义函数前置/后置处理器?,angular,refactoring,Angular,Refactoring,使用angular 8.2.14,我有一个angular服务通过HttpClient进行一些http调用。我使用一个行为主体和一个相关的可观察对象来说明http调用是否已经收到响应。这样,我的html页面可以在等待响应时显示mat progress微调器 这是我的服务的一个片段 export class myService { private waitingSubject = new BehaviorSubject<boolean>(false); readonly

使用angular 8.2.14,我有一个angular服务通过HttpClient进行一些http调用。我使用一个行为主体和一个相关的可观察对象来说明http调用是否已经收到响应。这样,我的html页面可以在等待响应时显示mat progress微调器

这是我的服务的一个片段

export class myService {

    private waitingSubject = new BehaviorSubject<boolean>(false);
    readonly waiting$ = this.waitingSubject.asObservable();

    constructor(
        private http: HttpClient
    ) { }

    public call1(): Observable<Whatever1> {
        this.waitingSubject.next(true);
        return this.http.post<Whatever1>('path/to/getWhatever1', null)
          .pipe(finalize(() => this.waitingSubject.next(false)));
    }

    public call2(): Observable<Whatever2> {
        this.waitingSubject.next(true);
        return this.http.post<Whatever2>('path/to/getWhatever2', null)
          .pipe(finalize(() => this.waitingSubject.next(false)));
    }

    public call3(): Observable<Whatever3> {
        this.waitingSubject.next(true);
        return this.http.post<Whatever3>('path/to/getWhatever3', null)
          .pipe(finalize(() => this.waitingSubject.next(false)));
    }
    ...

}
导出类myService{
private waitingSubject=新行为subject(false);
readonly waiting$=this.waitingSubject.asObservable();
建造师(
私有http:HttpClient
) { }
public call1():可观察{
this.waitingSubject.next(true);
返回此.http.post('path/to/getWhatever1',null)
.pipe(finalize(()=>this.waitingSubject.next(false));
}
公共调用2():可观察{
this.waitingSubject.next(true);
返回此.http.post('path/to/getWhatever2',null)
.pipe(finalize(()=>this.waitingSubject.next(false));
}
公共调用3():可观察{
this.waitingSubject.next(true);
返回此.http.post('path/to/getWhatever3',null)
.pipe(finalize(()=>this.waitingSubject.next(false));
}
...
}
然后,使用此服务的组件可以在html页面中显示微调器

<mat-progress-spinner *ngIf="myService.waiting$ | async" color="primary" mode="indeterminate"></mat-progress-spinner>

我是否有可能删除“this.waitingSubject.next(true);”和“.pipe(finalize(()=>this.waitingSubject.next(false))”,并在某个地方定义call1、call2、…、callX必须由next(true)预处理,并由pipe finalize位后处理

这只是一个因式分解的问题,因为我的代码是按原样工作的,但是我在任何地方都找不到任何与这个想法相关的东西


提前谢谢

您可以尝试定义一个函数来设置
waitingSubject
。试试下面的方法

导出类myService{
private waitingSubject=新行为subject(false);
readonly waiting$=this.waitingSubject.asObservable();
私人服务员=(呼叫:可观察)=>
水管(
点击(=>this.waitingSubject.next(true)),
完成(()=>this.waitingSubject.next(false))
)
构造函数(私有http:HttpClient){}
public call1():可观察{
返回this.water(this.http.post('path/to/getWhatever1',null));
}
公共调用2():可观察{
返回this.water(this.http.post('path/to/getWhatever2',null));
}
公共调用3():可观察{
返回this.water(this.http.post('path/to/getWhatever3',null));
}
...
}

注意:未经测试的代码,可能需要解决一些问题。

Michael D提出的建议在一个小改动后生效:

他的建议

private waiter = (call: Observable<any>) => 
    call.pipe(
      tap(_ => this.waitingSubject.next(true)),
      finalize(() => this.waitingSubject.next(false))
    )
私人服务员=(呼叫:可观察)=>
水管(
点击(=>this.waitingSubject.next(true)),
完成(()=>this.waitingSubject.next(false))
)
修改为

private waiter = (call: Observable<any>) => {
    this.waitingSubject.next(true);
    return call.pipe(
      finalize(() => this.waitingSubject.next(false))
    )
}
私人服务员=(呼叫:可观察)=>{
this.waitingSubject.next(true);
回电(
完成(()=>this.waitingSubject.next(false))
)
}

我喜欢使用operador来显示加载:非常有趣的线程@Eliseo,谢谢你的链接