angular - 当服务实际存在于提供者中时,没有提供服务

标签 angular angular2-services

我有一个服务,我正在其中调用另一个服务

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!

enter image description here

出了什么问题?

当我在 ngmodule 中添加提供程序时,它无论如何都会抛出该错误

我的应用程序模块:

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 中未提供的服务注入(inject) 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 {}

关于angular - 当服务实际存在于提供者中时,没有提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356609/

相关文章:

javascript - JSON 响应位于数组中,但仍引发错误 "NgFor only supports binding to Iterables such as Arrays"

html - 如何在元素中强制状态悬停

angular - 多次点击 URL 最多一分钟

angular - 如何在 Angular 2 中使用 ngModel 过滤空值?

json - 自动将 JSON 对象映射到 TypeScript 对象(Angular 2)

javascript - 组件中无法访问 Angular 2 提供程序

订阅的 Angular 返回值

javascript - Angular2 - 登录后重定向

javascript - 在 Angular2 中引用 HTML 中的对象

unit-testing - 测试可观察对象 Angular 2 karma