当服务实际存在于提供者中时,没有服务提供者
no provider for service when it actually exists in providers
我有这项服务,我在其中调用我的另一项服务
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]
但是当我 运行 应用程序时它抛出一个错误:
EXCEPTION: Error in ./LayoutComponent class LayoutComponent_Host - inline template:0:0 caused by: No provider for 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 { }
布局组件:
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) { }
}
我遇到过类似的问题。
对我来说,这是由于将 root 中未提供的服务注入 root 中提供的解析器造成的。
// 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 {}
我有这项服务,我在其中调用我的另一项服务
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]
但是当我 运行 应用程序时它抛出一个错误:
EXCEPTION: Error in ./LayoutComponent class LayoutComponent_Host - inline template:0:0 caused by: No provider for 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 { }
布局组件:
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) { }
}
我遇到过类似的问题。 对我来说,这是由于将 root 中未提供的服务注入 root 中提供的解析器造成的。
// 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 {}