我有这项服务,我正在拨打我的另一项服务
import { Injectable, Inject } from '@angular/core';
import { HttpCall } from './httpcall.service';
@Injectable()
export class SearchService {
constructor( @Inject(HttpCall) private httpCall: HttpCall) { }
}
在我的组件中
providers: [ HttpCall, LanguageServiceService, CookiesService, SearchService]
但是当我运行app时会抛出错误:
EXCEPTION :./LayoutComponent类中的错误LayoutComponent_Host - 内联模板:0:0引起:没有HttpCall的提供者!
出了什么问题?
当我在ngmodule
中添加提供程序时,无论如何都会抛出该错误
我的app.module:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
***
import { HttpCall } from './Service/httpcall.service';
import { SearchService } from './Service/search.service';
***
@NgModule({
declarations: [
LayoutComponent,
HomeComponent,
***
],
imports: [
BrowserModule,
ChartModule,
FormsModule,
ReactiveFormsModule,
HttpModule,
TranslateModule.forRoot(),
appRouting,
DatePickerModule,
BusyModule,
SelectModule
],
providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }, HttpCall, SearchService],
bootstrap: [LayoutComponent]
})
export class AppModule { }
LayoutComponent:
import { Component, OnInit, Input } from '@angular/core';
import { LanguageServiceService } from '../../Service/language-service.service';
import { CookiesService } from '../../Service/cookies.service';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Search } from './search';
import { HttpCall } from '../../Service/httpcall.service';
import { Router } from '@angular/router';
import { SearchService } from '../../service/search.service';
@Component({
selector: 'app-layout',
templateUrl: './layout.component.html',
styleUrls: ['./layout.component.css'],
providers: [LanguageServiceService, CookiesService]
})
export class LayoutComponent implements OnInit {
searchval: FormGroup;
searchModel: Search;
constructor(public ls: LanguageServiceService, public httpCall: HttpCall,
public router: Router, private sharedResource: SearchService) {
}
和我的孩子组成部分:
import { Component, OnInit } from '@angular/core';
import { HttpCall } from '../../Service/httpCall.Service';
import { LanguageServiceService } from './../../Service/language-service.service';
import { Router } from '@angular/router';
import { Reports } from './Reports';
@Component({
selector: 'app-reports',
templateUrl: './reports.component.html',
styleUrls: ['./reports.component.css'],
providers: [LanguageServiceService]
})
export class ReportsComponent implements OnInit {
busy: any;
reports: Reports[];
constructor(private httpCall: HttpCall, public languageService: LanguageServiceService, public router: Router) { }
}
答案 0 :(得分:2)
我遇到了类似的问题。 对我来说,这是由于向根目录中提供的解析器注入了根目录中未提供的服务。
// resolver
@Injectable({ providedIn: 'root' }) // remove providedIn and add to module's providers instead
export class SomeResolver implements
Resolve<any> { constructor(service: SomeService) { ... } }
// service
@Injectable()
export class SomeService {}
// module
@NgModule({
providers: [SomeService] // this won't provide service in root injectables
})
export class SomeModule {}