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

sto*_*oph 3 dependency-injection circular-dependency angular-directive angular

我创建了一个带有库的新 Angular 7 项目。该库包含一个指令、一个服务和一个模块(指令 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。指令.ts -> 项目\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
  ) { }
...
Run Code Online (Sandbox Code Playgroud)

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 }
      ]
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

登录表单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
  ) { }
...
Run Code Online (Sandbox Code Playgroud)

我不知道这个问题的原因...

小智 5

正如错误所说,存在循环依赖:

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

OAUTH_CONFIGURATION从哪里导出,从哪里auth.module导入auth.service,然后auth.service导入到auth.module指令中的。

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

令牌.ts:

export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');
Run Code Online (Sandbox Code Playgroud)

其它文件:

import { OAUTH_CONFIGURATION } from './token';
Run Code Online (Sandbox Code Playgroud)