Angular 4:无法实例化循环依赖!InjectionToken_HTTP_INTERCEPTORS

Vik*_*sal 8 javascript circular-dependency angular

我知道,这个问题可能听起来很复杂,我已经尝试了在stackover流程中发现的所有内容都无法解决这个问题,所以请耐心等待

为了让你能够重现错误,我为你提供了整个代码

Github Repo

问题

我收到以下错误:

提供者解析错误:↵无法实例化循环依赖!InjectionToken_HTTP_INTERCEPTORS("[ERROR - >]"):在./AppModule@-1:-1中的NgModule AppModule中

在此输入图像描述

有关场景的信息(注释)

注1 文件:response-interceptor.service.ts

路径: ./src/app/shared/interceptors/response-interceptor/

我正在拦截HTTPClient检查401错误的响应,当错误发生时我需要让用户重新登录.

要向用户显示重新登录提示,我已经创建了global-functions-services一个具有"relogin"功能的功能

注2 文件:global-function.service.ts

路径: ./src/app/shared/services/helper-services/global-function/

这是所有这一切开始发生的地方......我一注射了 PersonService

  constructor(
    public dialog: MatDialog,
    private _personService: PersonService
  ) { }
Run Code Online (Sandbox Code Playgroud)

我收到此错误,在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>;
  }
}
Run Code Online (Sandbox Code Playgroud)

请求

请为此提出一个解决方案,我已经浪费了2天时间来解决问题,但没有运气.

非常感谢!!提前

cod*_*ter 6

循环依赖,意味着无休止地盘旋,就像行星绕着太阳公转。

解决方案:打破依赖关系链,重构代码。

您具有GlobalFunctionService- > PersonService->等等...-> ResponseInterceptorService->并返回-> 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;
          });
        }
      }
Run Code Online (Sandbox Code Playgroud)


jit*_*der 5

您必须修改您的response-interceptor.service.ts

import { Injectable,Inject, Injector } from '@angular/core';

constructor( inj: Injector) { 
   this._globalFunctionService=inj.get(GlobalFunctionService)
 }
Run Code Online (Sandbox Code Playgroud)

您可以从此链接获取更多信息


小智 5

在构造函数中使用 setTimeout() 函数来分配服务。

constructor(private injector: Injector) {
  setTimeout(() => {
      this.loginService = this.injector.get(LoginService);
  })
}
Run Code Online (Sandbox Code Playgroud)

如果您遇到任何问题,请尝试此操作并恢复。