Twi*_*her 3 angular-universal angular angular-transfer-state
我正在尝试实现内置的TransferHttpCacheModule以消除重复请求。我在我的应用程序中使用这个拦截器:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authService = this.injector.get(AuthenticationService);
const url = `${this.request ? this.request.protocol + '://' + this.request.get('host') : ''}${environment.baseBackendUrl}${req.url}`
let headers = new HttpHeaders();
if (this.request) {
// Server side: forward the cookies
const cookies = this.request.cookies;
const cookiesArray = [];
for (const name in cookies) {
if (cookies.hasOwnProperty(name)) {
cookiesArray.push(`${name}=${cookies[name]}`);
}
}
headers = headers.append('Cookie', cookiesArray.join('; '));
}
headers = headers.append('Content-Type', 'application/json');
const finalReq: HttpRequest<any> = req.clone({ url, headers });
...
Run Code Online (Sandbox Code Playgroud)
它启用客户端的相对 URL 和服务器端的完整 URL,因为服务器不知道它自己的 URL。
问题是TransferHttpCacheModule使用基于方法、URL 和参数的密钥,并且服务器 URL 与客户端 URL 不匹配。
有没有办法强制TransferHttpCacheInterceptor在我自己的拦截器之前执行?我想避免在客户端强制使用完整的 URL。
您可以将拦截器放在自己的模块中:
@NgModule({
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: MyOwnInterceptor, multi: true }
]
})
export class MyOwnInterceptorModule {}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将此模块TransferHttpCacheModule放在 AppModule 内部的导入下方:
@NgModule({
imports: [
// ...
TransferHttpCacheModule,
MyOwnInterceptorModule
],
// ...
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
这样你的拦截器将在TransferHttpCacheInterceptor. 不过感觉很奇怪,因为据我所知,导入是排在第一位的,然后是提供者。通过这种方式,您可以覆盖导入中的提供程序。你确定你不想反过来吗?
| 归档时间: |
|
| 查看次数: |
1486 次 |
| 最近记录: |