【发布时间】:2020-07-08 01:46:56
【问题描述】:
我有这个服务,我正在调用我的另一个服务
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]
但是当我运行应用程序时,它会引发错误:
异常:./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 { }
布局组件:
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) { }
}
【问题讨论】:
-
那么您的组件提供程序和
@NgModule中都有HttpCall?从组件中移除它 -
@echonax 结果是一样的。实际上我只在组件中拥有它,所以我保持原样
-
从构造函数中移除
@Inject(HttpCall)部分。从组件中删除提供程序。将提供程序添加到@NgModule。这样不行吗? -
很遗憾没有。我在 app.module 中移动 httpcall 和 searchservice,现在在控制台中记录了没有提供 searchservice 的记录
-
分享您的模块和组件的完整代码。