Angular 7库-检测到循环依赖(指令,服务,模块)

时间:2019-02-27 12:59:53

标签: angular dependency-injection angular-directive circular-dependency

我用库创建了一个新的Angular 7项目。该lib包含一个指令,一个服务和一个模块(指令get是注入的服务,而服务是在模块中导出的injectionToken)。 编译时收到此警告:

  

检测到循环依赖项中的警告:projects \ auth \ src \ lib \ auth.module.ts->   projects \ auth \ src \ lib \ login-form-ref.directive.ts->   projects \ auth \ src \ lib \ auth.service.ts->   projects \ auth \ src \ lib \ auth.module.ts

     

警告:在循环依赖项中检测到:   projects \ auth \ src \ lib \ auth.service.ts->   projects \ auth \ src \ lib \ auth.module.ts->   projects \ auth \ src \ lib \ login-form-ref.directive.ts->   projects \ auth \ src \ lib \ auth.service.ts

     

警告:在循环依赖项中检测到:   projects \ auth \ src \ lib \ login-form-ref.directive.ts->   projects \ auth \ src \ lib \ auth.service.ts->   projects \ auth \ src \ lib \ auth.module.ts->   projects \ auth \ src \ lib \ login-form-ref.directive.ts

auth.service.ts

import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Token } from './models/token';
import { OAuthClient } from './models/oAuthClient';
import { OAUTH_CONFIGURATION } from './auth.module';


@Injectable({
  providedIn: 'root'
})
export class AuthService {
  expectedHttpResponse = 200;

  constructor(
    @Inject(OAUTH_CONFIGURATION) private readonly oAuthClient: OAuthClient
    , private readonly http: HttpClient
  ) { }
...

auth.module.ts

import { NgModule, InjectionToken } from '@angular/core';
import { LoginFormRefDirective } from './login-form-ref.directive';
import { ReactiveFormsModule } from '@angular/forms';
import { OAuthClient } from './models/oAuthClient';
import { HttpClientModule } from '@angular/common/http';

export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');

@NgModule({
  declarations: [LoginFormRefDirective],
  imports: [
    HttpClientModule,
    ReactiveFormsModule
  ],
  exports: [LoginFormRefDirective]
})
export class AuthModule {
  static forRoot(oAuthClientConfig: OAuthClient) {
    return {
      ngModule: AuthModule,
      providers: [
        { provide: OAUTH_CONFIGURATION, useValue: oAuthClientConfig }
      ]
    };
  }
}

login-form-ref.directive.ts

import { Directive, HostListener, Self, Input } from '@angular/core';
import { FormGroupDirective } from '@angular/forms';
import { Token } from './models/token';
import { AuthService } from './auth.service';

@Directive({
  // tslint:disable-next-line:directive-selector
  selector: '[loginFormRef]'
})
export class LoginFormRefDirective {
  @Input() storeTokenMethod: (token) => void;

  constructor(
    @Self() private readonly formGroup: FormGroupDirective
    , private readonly _authService: AuthService
  ) { }
...

我不知道该问题的原因...

1 个答案:

答案 0 :(得分:2)

如错误所示,存在循环依赖关系:

export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');

其中OAUTH_CONFIGURATION是从auth.module导出的,它被导入到auth.service中,而auth.service被导入到auth.module和该指令中。

尝试将Injection令牌放置在新文件中,然后将其导入auth.serviceauth.module

token.ts:

export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');

其他文件:

import { OAUTH_CONFIGURATION } from './token';