gpt4 book ai didi

html - Angular 9 : Cannot instantiate cyclic dependency

转载 作者:行者123 更新时间:2023-12-03 16:35:14 25 4
gpt4 key购买 nike

我正在重用 HTTP 通信服务,该服务在 Angular 8 下运行良好,但在 Angular 9 下抛出以下错误:

Error: Cannot instantiate cyclic dependency! HttpService
at throwCyclicDependencyError (core.js:8122)
at R3Injector.hydrate (core.js:17206)
at R3Injector.get (core.js:16960)
at NgModuleRef$1.get (core.js:36337)
at Object.get (core.js:33981)
at getOrCreateInjectable (core.js:5880)
at Module.ɵɵdirectiveInject (core.js:21115)
at NodeInjectorFactory.DashboardComponent_Factory [as factory] (dashboard.component.ts:9)
at getNodeInjectable (core.js:6025)
at instantiateRootComponent (core.js:12788)
at resolvePromise (zone-evergreen.js:798)
at resolvePromise (zone-evergreen.js:750)
at zone-evergreen.js:860
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:41640)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at Zone.runTask (zone-evergreen.js:167)
at drainMicroTaskQueue (zone-evergreen.js:569)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:484)
at invokeTask (zone-evergreen.js:1621)

这是 HTTP 服务的代码:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable()
export class HttpService {

url: string = 'http://localhost:8000/';

constructor(private httpClient: HttpClient) { }

public getTestCall() {
return this.httpClient.get(this.url + 'test');
}
}

这是使用 HTTP 服务的组件之一:
import { Component, OnInit } from '@angular/core';
import { HttpService } from 'src/app/services/http.service';

@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {

constructor(private http: HttpService) { }

ngOnInit(): void { }
}

这是我的 app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpService } from './services/http.service';
import { environment } from 'src/environments/environment';
import { DashboardComponent } from './views/dashboard/dashboard.component';
import { MaterialsComponent } from './views/materials/materials.component';
import { OrdersComponent } from './views/orders/orders.component';
import { CustomersComponent } from './views/customers/customers.component';
import { ModelsComponent } from './views/models/models.component';
import { ProductsComponent } from './views/products/products.component';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
declarations: [
AppComponent,
DashboardComponent,
MaterialsComponent,
OrdersComponent,
CustomersComponent,
ModelsComponent,
ProductsComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [
HttpService,
{ provide: 'BACKEND_API_URL', useValue: environment.backendApiUrl }
],
bootstrap: [AppComponent]
})
export class AppModule { }

使用 [routerLink] 指令和 app-routing.module.ts 在 app.component.html 中引用组件
提前致谢!

最佳答案

我更喜欢将 HTTP 服务声明为:@Injectable({
providedIn: 'root',
})
并且不在任何模块中提供它们。

这样,从一开始就可以在整个应用程序上访问服务,并且可能不会出现这样的错误。

如果您在问题中包含您的应用程序模块会更好。

关于html - Angular 9 : Cannot instantiate cyclic dependency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61864939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com