我知道,这个问题听起来很重复,我已经尝试了所有在stackover流上发现的问题,但无法解决此问题,请耐心等待

为了使您能够重现错误,我向您提供了整个代码,

Github Repo

问题

我收到以下错误:



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

有关方案的信息(注释)

注意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/
这是所有一切开始发生的地方...
我一注入(inject)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>;
  }
}

请求

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

非常感谢!!提前

最佳答案

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

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

您具有 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;
          });
        }
      }

09-03 19:49