我有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']);
});
});
Run Code Online (Sandbox Code Playgroud)
}
因此,当我使用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 …Run Code Online (Sandbox Code Playgroud) tl; dr:向下滚动到解决方案
我有一个循环依赖项,并且正在收到警告,这是正确的,但是,我正在对其进行管理。问题是我有一个聊天组件。在角落,您可以选择查看他们的个人资料页面,而在他们的个人资料页面中,您可以选择向他们发送消息,因此是循环依赖性。我正在通过管理
public async openProfile(): Promise<void> {
this.modalCtrl.dismiss(); //closing the chat component before opening the profile modal
const profile = await this.modalCtrl.create({
component: ProfileComponent,
});
await profile.present();
}
Run Code Online (Sandbox Code Playgroud)
public async openChat(): Promise<void> {
this.modalCtrl.dismiss(); //closing the profile component before opening the chat modal
const chat = await this.modalCtrl.create({
component: ProfileComponent,
});
await chat.present();
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来处理这种循环依赖关系?
更新:根据下面的建议,我尝试创建服务。但是现在我有了一个三向依赖圈:
private modalService: ModalService;
constructor(modalService: ModalService){
this.modalService = modalService
}
public async openProfile(): Promise<void> {
this.modalService.openProfile(this.userData);
}
Run Code Online (Sandbox Code Playgroud)
private modalService: ModalService; …Run Code Online (Sandbox Code Playgroud)