如何解决循环依赖

时间:2017-10-19 14:16:57

标签: angular

我有3项服务:

  

auth.service.ts,account.service.ts,http.service.ts

在用户注册时我应该创建新帐户,因此我将account.service.ts导入auth.service.ts。我应该这样做,因为我使用注册表单数据来创建一个新帐户。

@Injectable()
export class AuthService {
  constructor(public accountService: AccountService) {}

  signUp(name: string, phone: string, email: string, password: string): void {

    ...

  userPool.signUp(phone, password, attributeList, null, (err: any, result: any) => {
  if (err) {

    ...

    return;
  }

  this.accountService.createAccount(name, phone, email).subscribe(res => {

    ...

    this.router.navigate(['/auth/confirmation-code']);
  });
});

}

因此,当我使用AWS Cognito时,我应该将auth.service.ts中的授权令牌添加到http.service.ts     因此我将auth.service.ts导入http.service.ts。

@Injectable()
export class HttpService {
  private actionUrl: string;
  private headers: Headers;
  private options: RequestOptions;

  constructor(
    public _http: Http,
    public authService: AuthService 
  ) {
    this.actionUrl = 'https://example.com/dev';
    this.headers = new Headers();

    this.authService.getAuthenticatedUser().getSession((err: any, session: any) => {
      if(err) return;
      this.headers.append('Authorization', session.getIdToken().getJwtToken());
    });

    this.headers.append('Content-Type', 'application/json');
    this.headers.append('Accept', 'application/json');
    this.headers.append('Access-Control-Allow-Headers', 'Content-Type, X-XSRF-TOKEN');
    this.headers.append('Access-Control-Allow-Origin', '*');

    this.options = new RequestOptions({ headers: this.headers });
  }

    get(request: string): Observable<any> {
        return this._http.get(`${this.actionUrl}${request}`)
            .map(res => this.extractData(res))
            .catch(this.handleError);
   }

在我的account.service.ts中,我应该使用http.service.ts来创建新帐户。

@Injectable()
export class AccountService {
  constructor(public httpService: HttpService) {}
  

检测到循环依赖关系中的警告:   src / app / core / services / account.service.ts - &gt; src / app / core / services / http.service.ts - &gt; src / app / core / services / auth.service.ts - &gt; SRC /应用程序/核心/服务/ account.service.ts

     

检测到循环依赖关系中的警告:   src / app / core / services / auth.service.ts - &gt; src / app / core / services / account.service.ts - &gt; src / app / core / services / http.service.ts - &gt; SRC /应用程序/核心/服务/ auth.service.ts

     

检测到循环依赖关系中的警告:   src / app / core / services / http.service.ts - &gt; src / app / core / services / auth.service.ts - &gt; src / app / core / services / account.service.ts - &gt; SRC /应用程序/核心/服务/ http.service.ts

据我所知,这是循环依赖错误。 怎么解决?最佳实践? 所有服务都发挥其作用并且非常重要。

3 个答案:

答案 0 :(得分:6)

您可以使用Injector。像往常一样通过构造函数注入它,然后当您需要一些导致循环依赖的服务时,从中获取该服务。

class HttpService {
  constructor(private injector: Injector) { }

  doSomething() {
    const auth = this.injector.get(AuthService);
    // use auth as usual
  }
}

答案 1 :(得分:2)

@ v4.0.0起已弃用,请使用Type或InjectionToken

const auth = this.injector.get(AuthService);

在Angular 10上工作:

const AuthService = this.injector.get<AuthService>(AuthService);

答案 2 :(得分:0)

只是想分享一个发生在我身上的场景(也许它可以帮助某人)。所以我也遇到了同样的循环依赖错误和空注入器错误。唯一让我头晕目眩的是我只有一个服务,没有字面上的循环依赖。

所以我只是尝试查看 Null Injector 错误,问题是在我的 app.module 中我没有导入 HttpClientModule。一旦我将它导入到导入数组中,两个错误都得到了修复。

相关问题