简体   繁体   English

Angular Inject Service to Service:No Provider

[英]Angular Inject Service into Service : No Provider

I have two services in Angular: 我在Angular有两个服务:

MonitoringService.service.ts : MonitoringService.service.ts

import { ClientAppSettingService } from './clientAppSettings.service';
import { Injectable, Inject } from '@angular/core';

@Injectable()
export class MonitoringService
{
  constructor(private _clientAppSettingService: ClientAppSettingService)
  {
    this.getclientAppSettings();
  }
  getclientAppSettings(): any {
    this._clientAppSettingService.getConfig().subscribe(result => {
    this.data = result;
    }, error => console.error(error));
  }

and ClientAppSetting.service.ts : ClientAppSetting.service.ts

import { Injectable, Inject } from '@angular/core';
import { AppSettingsClient } from '../models/appSettingsClient';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class ClientAppSettingService {
  appUrl: string = "";
  constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    this.appUrl = baseUrl;
  }
  getConfig() {
    return this.http.get<AppSettingsClient>(this.appUrl + 'api/ClientAppSettings/Get');
  }
}

This is app.module.ts : 这是app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { ClientAppSettingService } from './services/clientAppSettings.service';
import { HomeService } from './services/home.service';
import { AppComponent } from './app.component';
import { MonitoringService } from './services/monitoring.service';
import { HomeComponent } from './home/home.component';
import { EmbedReportComponent } from './embedReport/embedReport.component';
import { BaseComponent } from './base.component';
@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    EmbedReportComponent,
    BaseComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot([
      { path: '', component: HomeComponent, pathMatch: 'full' },
      { path: 'report', component: EmbedReportComponent }
    ])
  ],
  providers: [
    ClientAppSettingService,
    HomeService,
    ReportService,
    MonitoringService
    ],
  bootstrap: [AppComponent]
})
export class AppModule { }

I followed this , which says that you need to provide service in the provider of NgModule . 我跟着这个 ,说你需要在NgModule的提供者中提供服务。

I also followed this , which says 我也跟着这个说了

Make sure you declare a provider for ClientAppSettingService before you declare a provider for MonitorningService 在为MonitorningService声明提供程序之前,请确保为ClientAppSettingService声明了一个提供程序

I also tried adding @Inject in my constructor as below: 我也尝试在我的构造函数中添加@Inject,如下所示:

constructor( @Inject(ClientAppSettingService) _clientAppSettingService: ClientAppSettingService)

However, I still receive error regarding No Provider: 但是,我仍然收到有关No Provider的错误:

ERROR Error: Uncaught (in promise): Error: No provider for ClientAppSettingService! 错误错误:未捕获(在承诺中):错误:没有ClientAppSettingService的提供程序! (MonitoringService -> ClientAppSettingService) Error: No provider for ClientAppSettingService! (MonitoringService - > ClientAppSettingService)错误:没有ClientAppSettingService的提供程序! (MonitoringService -> ClientAppSettingService) (MonitoringService - > ClientAppSettingService)

Additional Information: 附加信息:

I have a base.component.ts which calls the MonitoringService: 我有一个调用MonitoringService的base.component.ts

import { MonitoringService } from './services/monitoring.service';
import { Component, ReflectiveInjector, OnInit } from '@angular/core';

@Component({
  template: ''
})
export class BaseComponent
{
  constructor(private _monitoringService: MonitoringService)
  {
    const injector = ReflectiveInjector.resolveAndCreate([
      MonitoringService
    ]);
    this._monitoringService = injector.get(MonitoringService);
  }

Then I extent other components with Base.component.ts to use the MonitorningService as below. 然后我使用Base.component.ts其他组件以使用MonitorningService,如下所示。 For example home.component.ts uses MonitoringService as below: 例如, home.component.ts使用MonitoringService,如下所示:

import { Home } from '../models/home';
import { BaseComponent } from '../base.component';
import { MonitoringService } from '../services/monitoring.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html'
})

export class HomeComponent extends BaseComponent implements OnInit
{
  home: Home;

  constructor(private homeService: HomeService, private _monitorningService: MonitoringService)
  {
    super(_monitorningService);
  }

Let me explain it a little. 让我解释一下。 You have two layers one layer is your component and the other layer is your module. 您有两个图层,一个图层是您的组件,另一个图层是您的模块。 What you can do is provide the ClientAppSettingService into the component: 您可以做的是将ClientAppSettingService提供到组件中:

 @Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [ClientAppSettingService]
})

You have to remove it from your module and keep the other one there. 你必须将它从你的模块中删除,并保持另一个。 Keep in mind that everywhere where you want to inject your service you have to provide it into the component. 请记住,无论您想在哪里注入服务,都必须将其提供给组件。

This is happening because you have not registered your Service in app.module.ts file. 发生这种情况是因为您尚未在app.module.ts文件中注册您的服务。 You need to set ClientAppSettingService into providers array in app.module.ts. 您需要在app.module.ts中将ClientAppSettingService设置为providers数组。 it should look like 它应该看起来像

providers: [ClientAppSettingService]

of course you will have to import the same before using it into providers array. 当然,在将它用于providers数组之前,你必须导入相同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM