【问题标题】:Angular 7 Library - Circular Dependency Detected (Directive, Service, Module)Angular 7 库 - 检测到循环依赖(指令、服务、模块)
【发布时间】:2019-02-27 12:59:53
【问题描述】:

我创建了一个带有库的新 Angular 7 项目。该库包含一个指令、一个服务和一个模块(指令获取注入的服务和服务在模块中导出的 injectionToken)。 我在编译时收到此警告:

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

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

检测到循环依赖中的警告: 项目\auth\src\lib\login-form-ref.directive.ts-> 项目\auth\src\lib\auth.service.ts -> 项目\auth\src\lib\auth.module.ts -> 项目\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
  ) { }
...

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

【问题讨论】:

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


    【解决方案1】:

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

    export const OAUTH_CONFIGURATION = new InjectionToken&lt;OAuthClient&gt;('OAUTH_CONFIGURATION');

    其中OAUTH_CONFIGURATION是从auth.module导出的,auth.service是导入的,auth.serviceauth.module和指令导入的。

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

    token.ts:

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

    其他文件:

    import { OAUTH_CONFIGURATION } from './token';
    

    【讨论】:

    • 哦该死的..真可惜..感谢您的解决方案,它对我有用!
    • 很高兴听到它 :) 如果有效,请接受我的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多