Angular2:例外:没有 Angulartics2GoogleAnalytics 的提供者

Angular2 : EXCEPTION: No provider for Angulartics2GoogleAnalytics

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

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

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

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

EXCEPTION: No provider for Angulartics2GoogleAnalytics! Error: DI Error at NoProviderError.ZoneAwareError (zone.js:811) at NoProviderError.BaseError [as constructor] (core.umd.js:1186) at NoProviderError.AbstractProviderError [as constructor] (core.umd.js:1371) at new NoProviderError (core.umd.js:1411) at ReflectiveInjector_.throwOrNull (core.umd.js:3394) at ReflectiveInjector.getByKeyDefault (core.umd.js:3433) at ReflectiveInjector.getByKey (core.umd.js:3380) at ReflectiveInjector.get (core.umd.js:3140) at AppModuleInjector.NgModuleInjector.get (core.umd.js:8996) at CompiledTemplate.proxyViewClass.AppView.injectorGet (core.umd.js:12465) at CompiledTemplate.proxyViewClass.DebugAppView.injectorGet (core.umd.js:12845) at CompiledTemplate.proxyViewClass.View_AppComponent_Host0.createInternal (/AppModule/AppComponent/host.ngfactory.js:15) at CompiledTemplate.proxyViewClass.AppView.createHostView (core.umd.js:12421) at CompiledTemplate.proxyViewClass.DebugAppView.createHostView (core.umd.js:12829) at 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 { }

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

constructor(angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {}

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

constructor(public angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {}

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

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

试试这个。