简体   繁体   English

错误错误:未捕获(承诺):错误:NG0200:检测到 DI 中的循环依赖

[英]ERROR Error: Uncaught (in promise): Error: NG0200: Circular dependency in DI detected

I am working on an Angular app where I have 3 different components in the main app.我正在开发一个 Angular 应用程序,其中主应用程序中有 3 个不同的组件。 I have 3 different services for each of the component.我为每个组件提供 3 种不同的服务。 I am passing the data from component's service to the main service that is located in the main app, where I am fetching the data from the backend.我将数据从组件的服务传递到位于主应用程序中的主服务,在那里我从后端获取数据。 And, sending the data back from the main app service to the component's service in order to model the data that I have fetched.并且,将数据从主应用程序服务发送回组件的服务,以便对我获取的数据进行建模。 While doing this I am getting an issue.在这样做时,我遇到了一个问题。 Since, both of my services are somewhat dependent on each other I am getting an error of circular dependency.由于我的两个服务在某种程度上相互依赖,因此我收到了循环依赖错误。 How can I resolve this?我该如何解决这个问题?

Here's my code:这是我的代码:

app.service.ts应用服务.ts

import { ContactsService } from "./services/contacts.service";

constructor(private http: HttpClient, private contactsService: ContactsService){}
//I am getting an error probably due to creating an instance of contactsService

async postHTTPRequest(url: string)
{
const check = await this.readFile();
if(check == 1)
{
  this.tokenValue = "doJSUq6pR2RDgshFt9BYVmpzmRpTxVatuadEd5yV";
  this.http.post(this.baseURL+url, {token: this.tokenValue}).subscribe(response => {
    console.log(response);
    this.contactsService.modelData(response);
  })
}
}

contacts.service.ts联系人.service.ts

import { AppService } from "../app.service";

contactsURL: string = "/contact/getContacts";

constructor(private appService: AppService)
{
  this.appService.postHTTPRequest(this.contactsURL);
}

modelData(data: any)
{
  console.log("data received for modeling");
}

Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

EDIT: I have tried using a static function that can be called without creating any instance but, it results in getting an error when I have to pass the data from the static function to a non static function in order to use it in my component.html file.编辑:我曾尝试使用可以在不创建任何实例的情况下调用的静态函数,但是,当我必须将数据从静态函数传递到非静态函数以便在我的组件中使用它时,会导致出现错误。 .html 文件。 Also, global variable is not getting initialized with the data that the static function has.此外,全局变量没有使用静态函数拥有的数据进行初始化。

contacts.service.ts联系人.service.ts

static modelData(dataToModel: any)
{
   //function definition
}

app.service.ts应用服务.ts

this.http.post(this.baseURL+url {token:this.tokenValue})
 .subscribe(response => {
    ContactsService.modelData(response);
  }

Can anybody tell me how to resolve this?谁能告诉我如何解决这个问题?

app.service.ts应用服务.ts

import { ContactsService } from "./services/contacts.service";

constructor(
   private http: HttpClient, 
   @Inject(ContactsService) private contactsService
){

}

contacts.service.ts联系人.service.ts

import { AppService } from "../app.service";

constructor(@Inject(AppService) private appService)
{
  
}

The error message is clear: you have a circular dependency in your services.错误消息很明确:您的服务存在循环依赖关系。

AppService needs ContactService but ContactService needs AppService. AppService 需要 ContactService 但 ContactService 需要 AppService。 How could these services be constructed?如何构建这些服务?

Note that some framework are sometimes able to workaround circular dependencies but Angular is apparently not in this case.请注意,某些框架有时能够解决循环依赖问题,但 Angular 显然不是这种情况。

Circular dependency are often a sign of a poor design.循环依赖通常是糟糕设计的标志。

Solution: review your design to get rid of the circular dependency.解决方案:检查您的设计以摆脱循环依赖。

In your case IMHO, AppService should not depend on ContactService.在你的情况恕我直言,AppService 不应该依赖于 ContactService。 You could pass a parser method as parameter to postHTTPRequest or even make it only return an Observable and let the caller deal with the result itself.您可以将parser方法作为参数传递给postHTTPRequest ,甚至可以让它只返回一个Observable并让调用者自己处理结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ERROR 错误:未捕获(承诺中):错误:NG0200:为 UserService 检测到 DI 中的循环依赖 - ERROR Error: Uncaught (in promise): Error: NG0200: Circular dependency in DI detected for UserService NG0200:检测到 DI 中的循环依赖,为什么以及如何做? - NG0200: Circular dependency in DI detected,Why and How to do? NG0200:为 InjectionToken HTTP_INTERCEPTORS 检测到 DI 中的循环依赖 - NG0200: Circular dependency in DI detected for InjectionToken HTTP_INTERCEPTORS 将http定义为DI会给出EXCEPTION:未捕获(按承诺):错误:DI错误 - defining http as DI gives EXCEPTION: Uncaught (in promise): Error: DI Error Angular 2 No Provider或DI错误(未捕获(承诺):错误:No Provider…) - Angular 2 No Provider or DI error (Uncaught (in promise): Error: No provider …) Angular2“t 没有提供者!” 和未捕获(承诺):错误:DI 错误 - Angular2 "No provider for t!" and Uncaught (in promise): Error: DI Error 未捕获(承诺):服务器错误 - Uncaught (in promise): Server error 角度5中的圆依赖性误差 - Circular dependency error in angular 5 DI与ng-bootstraps错误 - DI with ng-bootstraps error 添加ng-toolkit / universal后出现错误-错误{错误:未捕获(承诺):错误 - Error after added ng-toolkit/universal - ERROR { Error: Uncaught (in promise): Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM