【问题标题】:Angular2 : EXCEPTION: No provider for Angulartics2GoogleAnalyticsAngular2:例外:没有 Angulartics2GoogleAnalytics 的提供者
【发布时间】:2017-01-29 07:53:26
【问题描述】:

我正在尝试在我的应用程序中使用 angulartics2

我已按照文档中的说明正确配置。

注意:没有提到将提供程序添加为依赖项的地方..

当我尝试运行应用程序时,它显示如下错误。

例外:没有 Angulartics2GoogleAnalytics 的提供者!错误:DI 错误 在 NoProviderError.ZoneAwareError (zone.js:811) 在 NoProviderError.BaseError [作为构造函数] (core.umd.js:1186) 在 NoProviderError.AbstractProviderError [作为构造函数] (core.umd.js:1371) 在新的 NoProviderError (core.umd.js:1411) 在 ReflectiveInjector_.throwOrNull (core.umd.js:3394) 在 ReflectiveInjector.getByKeyDefault (core.umd.js:3433) 在 ReflectiveInjector.getByKey (core.umd.js:3380) 在 ReflectiveInjector.get (core.umd.js:3140) 在 AppModuleInjector.NgModuleInjector.get (core.umd.js:8996) 在 CompiledTemplate.proxyViewClass.AppView.injectorGet (core.umd.js:12465) 在 CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (core.umd.js:12845) 在 CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/AppModule/AppComponent/host.ngfactory.js:15) 在 CompiledTemplate.proxyViewClass.AppView.createHostView (core.umd.js:12421) 在 CompiledTemplate.proxyViewClass.DebugAppView.createHostView (core.umd.js:12829) 在 ComponentFactory.create (core.umd.js:7766) 1: https://github.com/angulartics/angulartics2

App.component.ts

import { Component } from '@angular/core';
import { Angulartics2GoogleAnalytics } from 'angulartics2';
@Component({
  selector: 'my-app',
  moduleId: module.id,
  templateUrl: `./app.component.html`,
  styleUrls: ['/app.componenet.css']
})
export class AppComponent {
title = 'Angular2 google analytics';
constructor(angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {}
}

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';
import { HttpModule } from '@angular/http';
import { FormsModule }   from '@angular/forms';
import { Angulartics2Module, Angulartics2GoogleAnalytics } from 'angulartics2';
@NgModule({
  imports:      [ HttpModule, BrowserModule, FormsModule, Angulartics2Module.forRoot([ Angulartics2GoogleAnalytics ])],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
})
export class AppModule { }

【问题讨论】:

  • 是的,应该像你做的那样做。如果模块重复,则可能会发生这种情况,Angulartics2GoogleAnalytics 指的是不同模块中的不同类。你有App.component.ts,但是导入./app.component,这可能说明你对大小写不小心,会导致模块重复。我建议检查 Angulartics2GoogleAnalytics 在所有模块中是否真的是同一个类,正如在欺骗问题的答案中所建议的那样。
  • @estus 没用!我已经尝试了 2 个小时
  • 为了确保,'it'` 你的意思是你试图在两个类中将Angulartics2GoogleAnalytics 公开为window 属性,并且当你比较它时=== 相等?如果您不能提供可以复制问题的MCVE,我认为这里没有任何建议作为答案。

标签: javascript angular angulartics angulartics2


【解决方案1】:

使用providers 在组件中启动您的提供程序。

 @Component({
    selector: 'yourselector',
    styleUrls: [''],
    templateUrl: '',
    providers: [Angulartics2GoogleAnalytics]
})

试试这个。

【讨论】:

  • 提供程序块是必需的!
【解决方案2】:

您的问题应该存在于您的构造函数中:

constructor(angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {}

你应该在你的提供者前面添加例如public

constructor(public angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {}

【讨论】:

  • 奇怪的是,在注入器中公开这个也是必需的。我将其设为私有,然后将其转为公开修复。
猜你喜欢
  • 2016-04-18
  • 2016-04-06
  • 2017-01-31
  • 2017-07-21
  • 2016-12-18
  • 2016-07-06
  • 1970-01-01
  • 2016-02-16
  • 2016-11-27
相关资源
最近更新 更多