Angular HTTP Interceptor - Display spinner in multi-module app

后端 未结 4 1519
既然无缘
既然无缘 2020-12-30 11:01

I\'m trying to display the ng4-loading-spinner spinner for HTTP calls made to my API.

I based my code on the examples in the following links:

4条回答
  •  抹茶落季
    2020-12-30 12:03

    For everyone following up to this issue, the OP's code now is working fine, except for the remaining issue that the loader doesn't seem to hide. The fix to that is to subscribe to the Observable, after the .catch .do chain, like so:

    handleObs
        .catch((err: any) => {
            this.count--;
            return Observable.throw(err);
        })
        .do(event => {
            if (event instanceof HttpResponse) {
                this.count--;
                if (this.count == 0) this.spinner.hide();
            }
        })
        .subscribe(); /* <---------- ADD THIS */
    
    return handleObs;
    

    After this, the code should be working fine, and the loader will hide when the counter reaches 0. Thanks to all the above answers for their contribution as well!

提交回复
热议问题