我收到此错误:
无法实例化循环依赖!数据服务
我从一开始就在一个简单的测试应用程序中进行构建。我见过类似的问题,但是我不明白如何将他们的解决方案应用于我的问题。
代码:
数据服务
export class DataService {
constructor(private http: HttpClient) { }
getall(){
return this.http.get('https://jsonplaceholder.typicode.com/users');
}
}
此模块的主要组件
export class MainComponent implements OnInit {
constructor(private dataService:DataService) { } ----> this is causing the error but I need the service to feed all other components via input
ngOnInit(): void {
}
}
RootModule TS
@NgModule({
declarations: [MainComponent,LeftComponent,RightComponent,ContentComponent],imports: [
Commonmodule,RootRoutingModule
]
})
export class RootModule { }
AppRoutingModule:
const rootModule = () => import('./root/root.module').then(root=>root.RootModule);
const routes: Routes = [
{path: '',component: ShellComponent,children: [
{path: 'main',loadChildren: rootModule},{path: '',loadChildren: rootModule}
]
}
];
和AppModule:
@NgModule({
declarations: [
AppComponent,NavbarComponent,ShellComponent
],imports: [
BrowserModule,AppRoutingModule
],providers: [HttpClient,DataService],bootstrap: [AppComponent]
})
有人可以帮助我了解此循环依赖性错误吗?当我将DataService注入所谓的MainComponent时抛出该错误,MainComponent是此模块的源组件。我没有在其他任何地方注入DataService,只是在应用模块中注册了它!