Javascript Angular 4:无法实例化循环依赖!InjectionToken_HTTP_INTERCEPTORS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47091872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 4: Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS
提问by Vikas Bansal
I know, this question may sound duplicate and I have tried everything found on stackover flow unable to resolve this problem, so please bear with me
我知道,这个问题可能听起来很重复,我已经尝试了在 stackover flow 上找到的所有方法都无法解决这个问题,所以请耐心等待
To make you able to reproduce the error I am providing you the whole code thought this
为了让您能够重现错误,我为您提供了整个代码
Problem
问题
I am getting the following error:
我收到以下错误:
Provider parse errors:?Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
提供程序解析错误:?无法实例化循环依赖!InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): 在 NgModule AppModule in ./AppModule@-1:-1
Information about the scenario (Notes)
场景信息(备注)
Note 1File: response-interceptor.service.ts
注 1文件:response-interceptor.service.ts
Path: ./src/app/shared/interceptors/response-interceptor/
小路: ./src/app/shared/interceptors/response-interceptor/
I am intercepting the HTTPClientresponses to check the 401error and when the error comes I need to ask user to re-login.
我正在拦截HTTPClient响应以检查401错误,当错误出现时,我需要要求用户重新登录。
To show the re-login prompt to user I have made a global-functions-servicesthat has a function 'relogin'
为了向用户显示重新登录提示,我制作了global-functions-services一个具有“重新登录”功能的
Note 2File: global-function.service.ts
注 2文件:global-function.service.ts
Path: ./src/app/shared/services/helper-services/global-function/
小路: ./src/app/shared/services/helper-services/global-function/
Here is the place where this all started to happen...
As soon as I am injecting the PersonService
这就是这一切开始发生的地方......一旦我注射 PersonService
constructor(
public dialog: MatDialog,
private _personService: PersonService
) { }
I am getting this error and in PersonServiceI cannot find any importthat can cause the issue.
我收到此错误,在PersonService 中我找不到任何import可能导致此问题的错误。
PersonService:./src/app/shared/services/api-services/person/person.service.ts
人员服务:./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>;
}
}
Request
要求
Please suggest a solution for this, I have already wasted 2 days to figure out the issue but no luck.
请为此提出一个解决方案,我已经浪费了 2 天的时间来找出问题,但没有运气。
Many thanks!! in advance
非常感谢!!提前
采纳答案by codeSetter
Cyclic dependency, means circling around endless, like planets orbiting sun..
循环依赖,意味着无限循环,就像行星绕太阳运行一样。
Solution: Break the dependency chain, Re-factor code.
解决方案:打破依赖链,重构代码。
You have GlobalFunctionService-> PersonService -> so on... -> ResponseInterceptorService -> and back to -> GlobalFunctionService.
Cycle complete.
您有GlobalFunctionService-> PersonService -> 等等... -> ResponseInterceptorService -> 并返回 -> GlobalFunctionService。
循环完成。
REMOVE the PersonService dependency from GlobalFunctionService. (its not used anyway, if you need it then find different way to get around.)
从 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;
});
}
}
回答by jitender
You have to modify your response-interceptor.service.ts
你必须修改你的 response-interceptor.service.ts
import { Injectable,Inject, Injector } from '@angular/core';
constructor( inj: Injector) {
this._globalFunctionService=inj.get(GlobalFunctionService)
}
You can get more info From this link
您可以从此链接获取更多信息
回答by Jayendra Bariya
Use setTimeout() function in constructor to assign service.
在构造函数中使用 setTimeout() 函数来分配服务。
constructor(private injector: Injector) {
setTimeout(() => {
this.loginService = this.injector.get(LoginService);
})
}
Try this and revert back if you face any issue.
如果您遇到任何问题,请尝试此操作并返回。


