我知道,这个问题可能听起来很复杂,我已经尝试在stackover flow上找到的所有内容无法解决此问题,所以请耐心等待我
为了让您能够重现错误,我为您提供了整个代码,并认为这是
问题
我收到以下错误:
提供程序解析错误:↵无法实例化循环依赖! InjectionToken_HTTP_INTERCEPTORS(“[ERROR - >]”):在NgModule AppModule中 在./AppModule@-1:-1
有关该方案的信息(注释)
注1 档案:response-interceptor.service.ts
路径:./src/app/shared/interceptors/response-interceptor/
我正在拦截HTTPClient
个响应以检查401
错误,当错误发生时,我需要让用户重新登录。
要向用户显示重新登录提示,我创建了一个具有“relogin”功能的global-functions-services
注2 档案:global-function.service.ts
路径:./src/app/shared/services/helper-services/global-function/
以下是这一切开始发生的地方......
我一注射PersonService
constructor(
public dialog: MatDialog,
private _personService: PersonService
) { }
我收到此错误并在PersonService中找不到任何导致此问题的import
。
PersonService :
./src/app/shared/services/api-services/person/person.service.ts
import { IRequest } from './../../../interfaces/I-request';
import { environment } from 'environments/environment';
import { Injectable } from '@angular/core';
// for service
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/toPromise';
// models
import { Person } from 'app/shared/models/person';
import { RequestFactoryService, REQUEST_TYPES } from 'app/shared/factories/request-factory/request-factory.service';
@Injectable()
export class PersonService {
private _requestService: IRequest;
constructor(
_requestFactoryService: RequestFactoryService
) {
this._requestService = _requestFactoryService.getStorage(REQUEST_TYPES.HTTP);
}
public signup(record): Promise<Person> {
let url = environment.api + 'person/public/signup';
return this._requestService.post(url, record) as Promise<Person>;;
}
public authenticate(code: string, password: string): Promise<Person> {
let url = environment.api + 'auth/signin';
let postData = {
code: code,
password: password
}
return this._requestService.post(url, postData) as Promise<Person>;
}
}
请求
请为此建议一个解决方案,我已经浪费了2天时间来解决问题,但没有运气。
非常感谢!!提前
答案 0 :(得分:4)
循环依赖,意味着在无尽的周围盘旋,就像行星绕太阳运行一样..
解决方案:打破依赖链,重新计算代码。
您有 GlobalFunctionService - &gt; PersonService - &gt;等...... - &gt; ResponseInterceptorService - &gt;并返回 - &gt;的 GlobalFunctionService 即可。
周期完成。
从GlobalFunctionService中删除PersonService依赖项。 (无论如何,如果你需要它,它会被找到不同的方式。)
import { PersonService } from 'app/shared/services/api-services/person/person.service';
import { Injectable } from '@angular/core';
import { InputModalComponent } from 'app/shared/components/input-modal/input-modal.component';
import { MatDialog } from '@angular/material';
@Injectable()
export class GlobalFunctionService {
constructor(
public dialog: MatDialog
) { }
relogin(): void {
let dialogRef = this.dialog.open(InputModalComponent, {
width: '250px',
data: { title: "Please provide your password to re-login." }
});
dialogRef.afterClosed().subscribe(result => {
debugger
console.log('The dialog was closed');
let password = result;
});
}
}
答案 1 :(得分:2)
你必须修改你的response-interceptor.service.ts
import { Injectable,Inject, Injector } from '@angular/core';
constructor( inj: Injector) {
this._globalFunctionService=inj.get(GlobalFunctionService)
}
您可以获得更多信息From this link
答案 2 :(得分:0)
在构造函数中使用setTimeout()函数来分配服务。
constructor(private injector: Injector) {
setTimeout(() => {
this.loginService = this.injector.get(LoginService);
})
}
尝试此操作,如果遇到任何问题,请退回。