Angular 4: Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS

后端 未结 3 1163
生来不讨喜
生来不讨喜 2020-12-18 22:30

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

To make

相关标签:
3条回答
  • 2020-12-18 22:43

    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.

    REMOVE the PersonService dependency from GlobalFunctionService. (its not used anyway, if you need it then find different way to get around.)

          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;
              });
            }
          }
    
    0 讨论(0)
  • 2020-12-18 22:44

    Use setTimeout() function in constructor to assign service.

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

    Try this and revert back if you face any issue.

    0 讨论(0)
  • 2020-12-18 22:44

    You have to modify your 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

    0 讨论(0)
提交回复
热议问题