我有多个角度项目,每个项目都有一个调用相同 API 的服务。我一直在向请求添加一个标头,以确定从哪个应用程序发送了请求。所以我一直在尝试创建一个具有基本服务类的库来设置标头值
@Injectable({ providedIn: "root" })
export class AppbaseService {
scope: string;
constructor(@Inject(forwardRef(()=>'scope')) scope:string) {
this.scope = scope;
}
getScope():string{
return this.scope;
}
}
和一个http 拦截器,用于将该动态值作为标头附加到请求中
@Injectable()
export class ScopeInterceptor implements HttpInterceptor {
constructor(private appBaseService: AppbaseService) {
}
intercept(req: HttpRequest<any>,
next: HttpHandler): Observable<HttpEvent<any>> {
const clonedRequest = req.clone({
headers: req.headers.append(
'Scope',
this.appBaseService.scope
)
});
return next.handle(clonedRequest);
}
}
最后是一个将动态值设置为提供者的工厂
export function appValueFactory(baseService: AppbaseService) {
console.log(baseService);
return baseService.getScope();
}
但我不知道如何在模块中设置提供程序并低于错误,这不是很有帮助。
Uncaught Error: Can't resolve all parameters for qI: (?).
at Jt (main.js:1)
at e._getDependenciesMetadata (main.js:1)
at e._getFactoryMetadata (main.js:1)
at e.getProviderMetadata (main.js:1)
at main.js:1
at Array.forEach (<anonymous>)
at e._getProvidersMetadata (main.js:1)
at main.js:1
at Array.forEach (<anonymous>)
at e._getProvidersMetadata (main.js:1)
有人可以指出我正确的方向吗?
谢谢你。