Angular 拦截器没有在请求中添加自定义参数
Angular interceptor is not adding custom param in requests
我使用 Angular 9,我想在所有发往后端的请求中添加国家/地区名称。
我找到了一篇解释如何使用拦截器添加请求参数的博客
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
} from "@angular/common/http"
import { Observable } from "rxjs"
export class CountryInterceptorService implements HttpInterceptor {
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const cloneReq = req.clone({
params: req.params.set(
"country",
"France"
),
})
return next.handle(cloneReq)
}
}
这是我的app.modules
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
NgxShimmerLoadingModule,
SplashScreenModule,
AlertModule.forRoot(),
HttpClientModule,
SimpleNotificationsModule.forRoot(),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
HighlightModule,
LightboxModule,
ClipboardModule,
AppRoutingModule,
InlineSVGModule.forRoot(),
NgbModule,
],
providers: [
CookieService,
{
provide: HIGHLIGHT_OPTIONS,
useValue: {
coreLibraryLoader: () => import('highlight.js/lib/core'),
languages: {
xml: () => import('highlight.js/lib/languages/xml'),
typescript: () => import('highlight.js/lib/languages/typescript'),
scss: () => import('highlight.js/lib/languages/scss'),
json: () => import('highlight.js/lib/languages/json')
},
},
},
IpInterceptorService,
{
provide : APP_INITIALIZER,
useFactory : getIpService,
deps: [IpInterceptorService, HttpClient],
multi : true
},
],
bootstrap: [AppComponent],
})
export class AppModule { }
export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http);
}
/**
* Call the service use to get the ip adress of the user, then the country
* @param ipInterceptorService
*/
export function getIpService(ipInterceptorService : IpInterceptorService) {
return ()=> ipInterceptorService.init();
}
但是没用。当我检查我的请求时,我没有参数中的国家/地区。我错过了什么?
提前致谢。
您忘记以正确的方式提供拦截器。
在相应的模块中,您需要像这样提供它:
{provide: HTTP_INTERCEPTORS ,useClass: CountryInterceptorService, multi:true}
我使用 Angular 9,我想在所有发往后端的请求中添加国家/地区名称。
我找到了一篇解释如何使用拦截器添加请求参数的博客
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
} from "@angular/common/http"
import { Observable } from "rxjs"
export class CountryInterceptorService implements HttpInterceptor {
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const cloneReq = req.clone({
params: req.params.set(
"country",
"France"
),
})
return next.handle(cloneReq)
}
}
这是我的app.modules
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
NgxShimmerLoadingModule,
SplashScreenModule,
AlertModule.forRoot(),
HttpClientModule,
SimpleNotificationsModule.forRoot(),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
}),
HighlightModule,
LightboxModule,
ClipboardModule,
AppRoutingModule,
InlineSVGModule.forRoot(),
NgbModule,
],
providers: [
CookieService,
{
provide: HIGHLIGHT_OPTIONS,
useValue: {
coreLibraryLoader: () => import('highlight.js/lib/core'),
languages: {
xml: () => import('highlight.js/lib/languages/xml'),
typescript: () => import('highlight.js/lib/languages/typescript'),
scss: () => import('highlight.js/lib/languages/scss'),
json: () => import('highlight.js/lib/languages/json')
},
},
},
IpInterceptorService,
{
provide : APP_INITIALIZER,
useFactory : getIpService,
deps: [IpInterceptorService, HttpClient],
multi : true
},
],
bootstrap: [AppComponent],
})
export class AppModule { }
export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
return new TranslateHttpLoader(http);
}
/**
* Call the service use to get the ip adress of the user, then the country
* @param ipInterceptorService
*/
export function getIpService(ipInterceptorService : IpInterceptorService) {
return ()=> ipInterceptorService.init();
}
但是没用。当我检查我的请求时,我没有参数中的国家/地区。我错过了什么?
提前致谢。
您忘记以正确的方式提供拦截器。
在相应的模块中,您需要像这样提供它:
{provide: HTTP_INTERCEPTORS ,useClass: CountryInterceptorService, multi:true}