检测到循环依赖项中的警告: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
) { }
...
我不知道该问题的原因...
答案 0 :(得分:2)
如错误所示,存在循环依赖关系:
export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');
其中OAUTH_CONFIGURATION
是从auth.module
导出的,它被导入到auth.service
中,而auth.service
被导入到auth.module
和该指令中。
尝试将Injection令牌放置在新文件中,然后将其导入auth.service
和auth.module
。
token.ts:
export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');
其他文件:
import { OAUTH_CONFIGURATION } from './token';