如何从正常服务访问 HttpInterceptor?
How can I get access to HttpInterceptor from normal service?
目前我需要创建两个相同 class 的实例来访问 HttpInterceptor
中的一些变量。有没有一种方法可以只用一个实例来解析提供者,并允许我在正常服务中使用相同的拦截器 class 作为拦截器?
*.module.ts
.
.
Providers:[
CustomInterceptor, // first class instance
{
Provide: HTTP_INTERCEPTORS,
UseClass: CustomInterceptor, // second class instance
multi: true,
}
],
.
.
http.service.ts
constructor(
interceptor: CustomInterceptor,
){}
interceptor.hasNext$.next(true);
CustomInterceptor.ts
hasNext$ = new BehaviourSubject(false);
当我在 http.service.ts
中调用拦截器时,我访问第一个 CustomInterceptor
,而我向 httpClient 发出的请求是 CustomInterceptor
的第二个实例。因此,我在 http.service.ts
中的 hasNext$.next(true)
在第二个中永远不会改变。
有人建议我应该使用,
Providers: [
CustomInterceptor,
{
provide: HTTP_INTERCEPTORS,
useValue: CustomInterceptor,
multi: true,
},
]
但是上面的代码会抛出错误,因为 HTTP_INTERCEPTORS token 需要 useClass 并且根据我的理解 class 相同 class.[=21= 的实例]
更新
刚刚找到一种解决此问题的方法。
http.service.ts
constructor(
@Inject(HTTP_INTERCEPTOS) protected interceptor: CustomInterceptor,
){}
interceptor.hasNext$.next(true);
你可以做与你想做的完全相反的事情。
创建一个包含 "shared" 部分 (hasNext$ = new BehaviourSubject(false);
) 的服务(或使用当前的 http 服务),然后将其注入 CustomInterceptor
.
无需在提供程序中实例化其他令牌即可使其工作的其他方法是在正常服务中手动注入 HTTP_INTERCEPTORS。
app.moudule.ts
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: CustomInterceptor,
multi: true,
}
],
http.service.ts
constructor (
@Inject(HTTP_INTERCEPTORS) private interceptors: HttpInterceptors,
) {
let interceptor = interceptors.find(x => x instanceOf CustomInterceptor);
interceptor.hasNext$.next(true); // this way we have access to the same interceptor
}
目前我需要创建两个相同 class 的实例来访问 HttpInterceptor
中的一些变量。有没有一种方法可以只用一个实例来解析提供者,并允许我在正常服务中使用相同的拦截器 class 作为拦截器?
*.module.ts
.
.
Providers:[
CustomInterceptor, // first class instance
{
Provide: HTTP_INTERCEPTORS,
UseClass: CustomInterceptor, // second class instance
multi: true,
}
],
.
.
http.service.ts
constructor(
interceptor: CustomInterceptor,
){}
interceptor.hasNext$.next(true);
CustomInterceptor.ts
hasNext$ = new BehaviourSubject(false);
当我在 http.service.ts
中调用拦截器时,我访问第一个 CustomInterceptor
,而我向 httpClient 发出的请求是 CustomInterceptor
的第二个实例。因此,我在 http.service.ts
中的 hasNext$.next(true)
在第二个中永远不会改变。
有人建议我应该使用,
Providers: [
CustomInterceptor,
{
provide: HTTP_INTERCEPTORS,
useValue: CustomInterceptor,
multi: true,
},
]
但是上面的代码会抛出错误,因为 HTTP_INTERCEPTORS token 需要 useClass 并且根据我的理解 class 相同 class.[=21= 的实例]
更新
刚刚找到一种解决此问题的方法。
http.service.ts
constructor(
@Inject(HTTP_INTERCEPTOS) protected interceptor: CustomInterceptor,
){}
interceptor.hasNext$.next(true);
你可以做与你想做的完全相反的事情。
创建一个包含 "shared" 部分 (hasNext$ = new BehaviourSubject(false);
) 的服务(或使用当前的 http 服务),然后将其注入 CustomInterceptor
.
无需在提供程序中实例化其他令牌即可使其工作的其他方法是在正常服务中手动注入 HTTP_INTERCEPTORS。
app.moudule.ts
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: CustomInterceptor,
multi: true,
}
],
http.service.ts
constructor (
@Inject(HTTP_INTERCEPTORS) private interceptors: HttpInterceptors,
) {
let interceptor = interceptors.find(x => x instanceOf CustomInterceptor);
interceptor.hasNext$.next(true); // this way we have access to the same interceptor
}