【问题标题】:no provider for service when it actually exists in providers实际存在于提供者中时,没有提供服务的提供者
【发布时间】: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 的记录
  • 分享您的模块和组件的完整代码。

标签: angular angular2-services


【解决方案1】:

我遇到了类似的问题。 对我来说,这是由于将 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 {}

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 2017-08-13
    • 2018-01-18
    • 2020-12-21
    • 2014-11-27
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多