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

问题描述 投票:1回答:1

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

检测到循环依赖项中的警告: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 }
      ]
    };
  }
}

登录外形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个回答
3
投票

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

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

其中OAUTH_CONFIGURATIONauth.module输出,导入auth.serviceauth.service输入auth.module和指令。

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

token.ts:

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

其他文件:

import { OAUTH_CONFIGURATION } from './token';
© www.soinside.com 2019 - 2024. All rights reserved.