如何解决循环依赖 [英] How to solve the circular dependency

查看:169
本文介绍了如何解决循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3种服务:

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

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

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

While user signup I should create new account therefore I imported account.service.ts to auth.service.ts. I should do it because I use signup form data for creating a new account.

@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.

So as I use AWS Cognito I should add an authorization token from auth.service.ts to http.service.ts therefore I imported auth.service.ts to 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创建新帐户.

In my account.service.ts I should use http.service.ts for creating new account.

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

警告:在循环依赖项中检测到: src/app/core/services/account.service.ts-> src/app/core/services/http.service.ts-> src/app/core/services/auth.service.ts-> src/app/core/services/account.service.ts

WARNING in Circular dependency detected: src/app/core/services/account.service.ts -> src/app/core/services/http.service.ts -> src/app/core/services/auth.service.ts -> src/app/core/services/account.service.ts

警告:在循环依赖项中检测到: src/app/core/services/auth.service.ts-> src/app/core/services/account.service.ts-> src/app/core/services/http.service.ts-> src/app/core/services/auth.service.ts

WARNING in Circular dependency detected: src/app/core/services/auth.service.ts -> src/app/core/services/account.service.ts -> src/app/core/services/http.service.ts -> src/app/core/services/auth.service.ts

警告:在循环依赖项中检测到: src/app/core/services/http.service.ts-> src/app/core/services/auth.service.ts-> src/app/core/services/account.service.ts-> src/app/core/services/http.service.ts

WARNING in Circular dependency detected: src/app/core/services/http.service.ts -> src/app/core/services/auth.service.ts -> src/app/core/services/account.service.ts -> src/app/core/services/http.service.ts

我知道这是循环依赖错误. 怎么解决呢?最佳实践? 所有服务都能发挥作用,而且很重要.

I understand that this is circular dependency Error. How to solve it? Best practice? All services fulfill their role and are important.

推荐答案

您可以为此使用Injector.照常通过构造函数将其注入,然后当您需要一些导致循环依赖的服务时,请从中获取该服务.

You can use Injector for this. Inject it via constructor as usual, and then when you will need some service that leads to the circular dependency, get that service from it.

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

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

这篇关于如何解决循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆