【问题标题】:angular 7 how to inject http service into another service角度7如何将http服务注入另一个服务
【发布时间】:2019-10-08 03:02:14
【问题描述】:

我正在尝试将我的HTTP 服务注入另一个服务。我不确定我做错了什么,我不断收到错误

无法解析 GanttPropertyService 的所有参数:(?)。

这是我的服务

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';


@Injectable({
    providedIn: 'root'
})

export class HttpService {

    constructor(private httpClient: HttpClient) {}

    ...
}

应用模块

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/shared/httpService';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpConfigInterceptor} from './services/shared/httpconfig.interceptor';
import {ErrorDialogService} from './services/shared/errorHandling/errorDialogueService'
import { GanttModule } from './gantt/gantt.module'

@NgModule({
  declarations: [
    AppComponent,
    ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    GanttModule,

  ],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: HttpConfigInterceptor, multi: true }, 
    HttpService,
    ErrorDialogService,    
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

我正在尝试在GanttModule 中使用这个httpService 甘特模块

import { NgModule } from '@angular/core';
import { GanttComponent } from './gantt.component';
import { GanttPropertyService} from '../services/gantt/ganttPropertyService';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatToolbarModule, MatButtonModule, MatMenuModule, MatIconModule, MatDialogModule } from '@angular/material';
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 
import { GanttToolBar } from './ganttToolBar/ganttToolBar.component';

@NgModule({
  declarations: [
    GanttComponent,
    GanttToolBar,
    ],
  imports: [
    FormsModule,
    ReactiveFormsModule,
    MatMenuModule,
    MatToolbarModule, MatButtonModule, MatIconModule, BrowserAnimationsModule,
    MatDialogModule        
  ],
  providers: [
    GanttPropertyService,

  ]
})
export class GanttModule { }

这是我要注入的服务

import {HttpService} from '../shared/httpService';


export class GanttPropertyService{

    constructor(private http : HttpService){}

}

如何在较低级别的模块中使用httpService?`

【问题讨论】:

  • 我没有投反对票,但有几件事。如果您将providedIn: 'root' 用于该服务,则无需将HttpService 添加到providers。还有@InjectableGanttPropertyService 在哪里?尝试将@Injectable({ providedIn: 'root' }) 添加到GanttPropertyService,如果这样可以解决问题,请告诉我们。

标签: angular dependency-injection


【解决方案1】:

尝试更新GanttPropertyService,使其成为@Injectable 服务,就像HttpService

import { Injectable } from '@angular/core';
import { HttpService } from '../shared/httpService';

@Injectable({
  providedIn: 'root'
})
export class GanttPropertyService {
  constructor(private http: HttpService){}
}

另外,当使用providedIn: 'root' 时,您确实不需要将相应的服务添加到模块的providers 数组中。这使服务成为singleton service

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-22
    • 2019-04-19
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多