Angular 提供商,循环依赖错误
Angular Provider, Cyclic Dependency Error
我收到这个错误:
提供程序解析错误:无法实例化循环依赖!
我有一个组件可以对我的后端进行 http 调用:
backend.component.ts
import { HttpClient } from '@angular/common/http';
public basicQuery(): void {
this.httpClient.get('http://...')
.subscribe(
(response) => {
},
error => {
});
}
我每次调用都使用拦截器。
token.interceptor.ts
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
token: this.auth.getToken()
}
});
return next
.handle(request)
.do(
(response: HttpEvent<any>) => {
},
(error: any) => {
return false;
});
}
如果出现错误,我会在我的自定义错误处理程序中进行处理,因为我想将错误发送到我的后端。
自定义错误-handler.ts
handleError(error): void {
this.errorLogService.logServer(error);
super.handleError(error);
}
为此我使用了一项服务。
errorLog.service.ts
import { HttpClient } from '@angular/common/http';
logServer(error: Error) {
this.httpClient.post('http://...', JSON.stringify(error.message))
.subscribe(
(response) => { },
(error) => { });
}
这就是问题所在,因为我也使用了 httpClient。
但是我怎样才能避免这种失败,如何做对呢?
现在我使用 'Http' 而不是 'HttpClient'.
更新 1
private auth: AuthService;
constructor(
private utils: Utils,
private router: Router,
private injector: Injector
) {
this.auth = this.injector.get(AuthService);
}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
token: this.auth.getToken()
}
});
return next
.handle(request)
.do(
(response: HttpEvent<any>) => { },
(error: any) => { return false }
);
}
更新 2
将 AuthService 和 Router 注入正文修复了问题:
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const started = Date.now();
this.auth = this.injector.get(AuthService);
this.router = this.injector.get(Router);
(...)
}
这里是你如何做到这一点,不要在构造函数中注入服务,而是在你的函数体中注入服务。在这里你也不会 运行 进入诸如达到最大呼叫数之类的问题。
@Injectable()
export class YourInterceptor implements HttpInterceptor {
yourService: YourService;
constructor(private inject: Injector) {
//this.yourService = inject.get(YourService);
}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
this.yourService = this.inject.get(YourService);
request = request.clone({
setHeaders: {
token: this.YourService.getToken()
}
});
return next.handle(request);
}
}
按照你的构造函数试试这个,
private auth: AuthService;
private router: Router;
constructor(
private utils: Utils,
private injector: Injector
) {
}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.router = this.injector.get(Router);
this.auth = this.injector.get(AuthService);
request = request.clone({
setHeaders: {
token: this.auth.getToken()
}
});
return next
.handle(request)
.do(
(response: HttpEvent<any>) => { },
(error: any) => { return false }
);
}
我收到这个错误:
提供程序解析错误:无法实例化循环依赖!
我有一个组件可以对我的后端进行 http 调用:
backend.component.ts
import { HttpClient } from '@angular/common/http';
public basicQuery(): void {
this.httpClient.get('http://...')
.subscribe(
(response) => {
},
error => {
});
}
我每次调用都使用拦截器。
token.interceptor.ts
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
token: this.auth.getToken()
}
});
return next
.handle(request)
.do(
(response: HttpEvent<any>) => {
},
(error: any) => {
return false;
});
}
如果出现错误,我会在我的自定义错误处理程序中进行处理,因为我想将错误发送到我的后端。
自定义错误-handler.ts
handleError(error): void {
this.errorLogService.logServer(error);
super.handleError(error);
}
为此我使用了一项服务。
errorLog.service.ts
import { HttpClient } from '@angular/common/http';
logServer(error: Error) {
this.httpClient.post('http://...', JSON.stringify(error.message))
.subscribe(
(response) => { },
(error) => { });
}
这就是问题所在,因为我也使用了 httpClient。 但是我怎样才能避免这种失败,如何做对呢? 现在我使用 'Http' 而不是 'HttpClient'.
更新 1
private auth: AuthService;
constructor(
private utils: Utils,
private router: Router,
private injector: Injector
) {
this.auth = this.injector.get(AuthService);
}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
token: this.auth.getToken()
}
});
return next
.handle(request)
.do(
(response: HttpEvent<any>) => { },
(error: any) => { return false }
);
}
更新 2
将 AuthService 和 Router 注入正文修复了问题:
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const started = Date.now();
this.auth = this.injector.get(AuthService);
this.router = this.injector.get(Router);
(...)
}
这里是你如何做到这一点,不要在构造函数中注入服务,而是在你的函数体中注入服务。在这里你也不会 运行 进入诸如达到最大呼叫数之类的问题。
@Injectable()
export class YourInterceptor implements HttpInterceptor {
yourService: YourService;
constructor(private inject: Injector) {
//this.yourService = inject.get(YourService);
}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
this.yourService = this.inject.get(YourService);
request = request.clone({
setHeaders: {
token: this.YourService.getToken()
}
});
return next.handle(request);
}
}
按照你的构造函数试试这个,
private auth: AuthService;
private router: Router;
constructor(
private utils: Utils,
private injector: Injector
) {
}
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.router = this.injector.get(Router);
this.auth = this.injector.get(AuthService);
request = request.clone({
setHeaders: {
token: this.auth.getToken()
}
});
return next
.handle(request)
.do(
(response: HttpEvent<any>) => { },
(error: any) => { return false }
);
}