admin管理员组文章数量:1332890
I am implementing a loading spinner that runs for all my http requests via an HttpInterceptorFn using this blog post as a guide: /
loading.interceptor.ts
export const loadingInterceptor: HttpInterceptorFn = (req, next) => {
const loadingSpinnerService = inject(LoadingSpinnerService);
console.log('SkipLoading: ', req.context.get(SkipLoading));
if (req.context.get(SkipLoading)) {
return next(req);
}
loadingSpinnerService.setIsLoading(true);
return next(req).pipe(
finalize(() => {
loadingSpinnerService.setIsLoading(false);
}),
);
};
HttpContextToken exported from the loading-spinner.service.ts
export const SkipLoading = new HttpContextToken<boolean>(() => false);
refreshToken API call where I wish to skip the spinner
refreshToken() {
return this.http.post<TokenResponse>(
this.apiUrl + '/Token/refresh/' + this.siteUserId$.value?.toString(),
{ context: new HttpContext().set(SkipLoading, true) },
);
}
Using console.log() I am noticing that my SkipLoading token is always false. Is there some difference in the implementation when using HttpInterceptorFn rather than HttpInterceptor class?
EDIT: After changing the API type to GET the context token evaluates to true as expected. But I must still be doing something wrong as I need this functionality for POST requests as well.
I am implementing a loading spinner that runs for all my http requests via an HttpInterceptorFn using this blog post as a guide: https://blog.angular-university.io/angular-loading-indicator/
loading.interceptor.ts
export const loadingInterceptor: HttpInterceptorFn = (req, next) => {
const loadingSpinnerService = inject(LoadingSpinnerService);
console.log('SkipLoading: ', req.context.get(SkipLoading));
if (req.context.get(SkipLoading)) {
return next(req);
}
loadingSpinnerService.setIsLoading(true);
return next(req).pipe(
finalize(() => {
loadingSpinnerService.setIsLoading(false);
}),
);
};
HttpContextToken exported from the loading-spinner.service.ts
export const SkipLoading = new HttpContextToken<boolean>(() => false);
refreshToken API call where I wish to skip the spinner
refreshToken() {
return this.http.post<TokenResponse>(
this.apiUrl + '/Token/refresh/' + this.siteUserId$.value?.toString(),
{ context: new HttpContext().set(SkipLoading, true) },
);
}
Using console.log() I am noticing that my SkipLoading token is always false. Is there some difference in the implementation when using HttpInterceptorFn rather than HttpInterceptor class?
EDIT: After changing the API type to GET the context token evaluates to true as expected. But I must still be doing something wrong as I need this functionality for POST requests as well.
Share Improve this question edited Nov 21, 2024 at 5:13 Naren Murali 58.9k5 gold badges44 silver badges77 bronze badges asked Nov 21, 2024 at 4:42 Asad KothsAsad Koths 918 bronze badges1 Answer
Reset to default 4For post
the syntax is given in the HttpClient - Angular.dev as:
post(url: string, body: any, options: { headers?: HttpHeaders |
{ [header: string]: string | string[]; } | undefined; context?: HttpContext | undefined;
observe?: "body" | undefined; params?: HttpParams | { ...; } | undefined;
reportProgress?: boolean | undefined; responseType: "arraybuffer"; withCredentials?: boolean | undefined;
transferCache?: boolean ...): Observable<ArrayBuffer>;
Notice that the second argument is the body of the post request, so the context
should be passed in an object as the third argument.
refreshToken() {
return this.http.post<TokenResponse>(
this.apiUrl + '/Token/refresh/' + this.siteUserId$.value?.toString(),
null, { context: new HttpContext().set(SkipLoading, true) },
);
}
本文标签: angularHttpContextToken always false while using HttpInterceptorFnStack Overflow
版权声明:本文标题:angular - HttpContextToken always false while using HttpInterceptorFn - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313352a2451321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论