I'm using ngx-progressbar and it works fine with http request started from within services, components or resolvers.
Note that no manual triggering of the progress bar (via service, etc) during http request is needed. It is triggered automatically.
Unfortunately it doesn't work as expected when making an http request from within an NGXS State :
I created for every case a button :
- "Make Request (Dispatch to store, w/o zone)"
This does not work, no progress bar appears (or it appears but hangs and does not complete to 100%)
@Action(LoadUrl)
load({ setState }: StateContext<string>) {
return this.http.get<any>('https://jsonplaceholder.typicode.com/posts/1').pipe(tap(res => console.log(res)));
}
- "Make Request (Component)"
This does work as expected, progress bar appears
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
// ...
makeRequestComponent() {
this.http.get<any>('https://jsonplaceholder.typicode.com/posts/1').pipe(tap(res => console.log(res))).subscribe();
}
}
- "Make Request (Dispatch to store, w/ zone)"
I asked the developer of ngx-progressbar and he actually found the issue and solved it by wrapping the http call inside zone. This does work, progress bar appears :
@State<string>({
name: 'testState',
defaults: ''
})
export class TestUrlState {
constructor(private http: HttpClient, private zone: NgZone) { }
@Action(LoadUrl)
load({ setState }: StateContext<string>) {
this.zone.run(() => {
this.http.get<any>('https://reqres.in/api/users?delay=2').pipe(
tap(res => console.log(res))
).subscribe();
});
}
}
Since I want the progress bar to appear with every http request, what would be the right approach to solve this ?
Is there a way to tell NGXS to run every http request inside zone ?