Angular 4: Unable to create circular dependency! InjectionToken_HTTP_INTERCEPTORS

I know this question may sound duplicate, and I tried everything found in the stack thread unable to solve this problem, so please bear with me

So that you can reproduce the error, I provide you with all the code, I thought it was

Github repo

Problem

I get the following error:

Vendor analysis errors: ↵ Unable to create a circular dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR →]"): in NgModule AppModule in./ AppModule @ -1 : -1

enter image description here

Script info (notes)

Note 1 File: response-interceptor.service.ts

Way: ./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
  ) { }

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 , , .

!

+4
3

, , , .

. , .

GlobalFunctionService → PersonService → ... → ResponseInterceptorService → → GlobalFunctionService.
.

PersonService GlobalFunctionService. ( , , .)

      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

setTimeout() .

constructor(private injector: Injector) {
  setTimeout(() => {
      this.loginService = this.injector.get(LoginService);
  })
}

, - .

+1

-interceptor.service.ts

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

constructor( inj: Injector) { 
   this._globalFunctionService=inj.get(GlobalFunctionService)
 }

0

Source: https://habr.com/ru/post/1688669/


All Articles