Angular 7 库 - 检测到循环依赖(指令、服务、模块)
Angular 7 Library - Circular Dependency Detected (Directive, Service, Module)
我用库创建了一个新的 Angular 7 项目。该库包含一个指令、一个服务和一个模块(指令获取注入的服务和在模块中导出的 injectionToken 服务)。
我在编译时收到此警告:
WARNING in Circular dependency detected: 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
WARNING in Circular dependency detected:
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
WARNING in Circular dependency detected:
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
) { }
...
我不知道那个问题的原因...
如错误所述,存在循环依赖:
export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');
其中 OAUTH_CONFIGURATION
从 auth.module
导出,导入到 auth.service
,auth.service
导入到 auth.module
和指令。
尝试将注入令牌放入新文件中,然后将其导入 auth.service
和 auth.module
。
token.ts:
export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');
其他文件:
import { OAUTH_CONFIGURATION } from './token';
我用库创建了一个新的 Angular 7 项目。该库包含一个指令、一个服务和一个模块(指令获取注入的服务和在模块中导出的 injectionToken 服务)。 我在编译时收到此警告:
WARNING in Circular dependency detected: 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
WARNING in Circular dependency detected: 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
WARNING in Circular dependency detected: 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
) { }
...
我不知道那个问题的原因...
如错误所述,存在循环依赖:
export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');
其中 OAUTH_CONFIGURATION
从 auth.module
导出,导入到 auth.service
,auth.service
导入到 auth.module
和指令。
尝试将注入令牌放入新文件中,然后将其导入 auth.service
和 auth.module
。
token.ts:
export const OAUTH_CONFIGURATION = new InjectionToken<OAuthClient>('OAUTH_CONFIGURATION');
其他文件:
import { OAUTH_CONFIGURATION } from './token';