首页 > 解决方案 > 为什么 Angular 在提供者列表中确实存在“”时不抛出任何提供者?

问题描述

我有以下可注射服务

import { ConfigService } from './app-config.service';
import { Injectable, OnInit, Inject } from '@angular/core';
import { AppInsights } from 'applicationinsights-js';

@Injectable()
export class AppInsightsService {
  appInsightsInstrumentationKey: string;

  private config: Microsoft.ApplicationInsights.IConfig = {
    instrumentationKey: this.appInsightsInstrumentationKey
  }

   constructor (private configService: ConfigService){
     this.configService = configService;

     this.appInsightsInstrumentationKey = 
     configService.config.appInsightsInstumentationKey;

     if (!AppInsights.config) {
       AppInsights.downloadAndSetup(this.config);
     }
  }

  logPageView(name?: string, url?: string, properties?: any, measurements?: any, duration?: number) {
    AppInsights.trackPageView(name, url, properties, measurements, duration);
  }

  logEvent(name: string, properties?: any, measurements?: any) {
    AppInsights.trackEvent(name, properties, measurements);
  }
}

ConfigService 在 app.module 的提供者列表中(实际上它被注入到其他类中就好了)


     providers: [
        LoaderService,
        ConfigService,
        {
          provide: APP_INITIALIZER,
          useFactory: loadConfig,
          deps: [ConfigService],
          multi: true
        },
        DaterangepickerConfig,
        TriggerEventAssignmentComponent,
        TimelineSummaryComponent,
        AlternateTemplateService,
        SelectedTriggerService,
        RouteHistoryService,
        AppInsightsService
      ],

将配置服务注入 AppInsightsService 但是我收到错误:

错误:没有 ConfigService 的提供者!(AppInsightsService -> ConfigService)

AppInsightsService 被注入到 app.component 构造函数中。

我无法注射到注射剂中还是这里发生了其他事情?

更新:我尝试添加到此 AppInsightsService 的构造函数中的任何内容都得到了这个。我试过像'Http'这样的东西,我得到同样的错误,说没有HTTP提供者。所以这与 AppInsightsSerivce 如何在角度中使用以及何时加载有关。我怀疑它在 App.Module 之前被加载了它的工作吗?

标签: angular

解决方案


推荐阅读