2017-01-29 7 views
1

私のアプリケーションにangulartics2を使用しようとしています。Angular2:例外:Angulartics2GoogleAnalyticsのプロバイダはありません

私は文書で述べたとおり正しく設定しました。

注:依存関係として、プロバイダを追加するために言及されない場所がありません。..

私はアプリケーションを実行しようとすると、それは以下のようなエラーが表示されます。

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 { } 
+1

[Angular 2 - プロバイダーを追加したにもかかわらずプロバイダ2のエラーが発生する可能性があります]](http://stackoverflow.com/questions/41796587/angular-2-no-provider-error-even-though -ive-added-a-provider) – estus

+0

はい、完了したようにする必要があります。これはモジュールが重複した場合に発生し、 'Angulartics2GoogleAnalytics'は異なるモジュールの異なるクラスを参照します。あなたは 'App.component.ts'を持っていますが、'。/ app.component'をインポートします。これはあなたが大文字小文字で不注意だったことを示している可能性があり、それはモジュールの重複につながります。 'Angulartics2GoogleAnalytics'がすべてのモジュールで実際に同じクラスであるかどうかを確認することをお勧めします。 – estus

+0

@estusいいえ、それはうまくいきません!私は2時間試しました – Sajeetharan

答えて

1

あなたのコンストラクタ内に存在する必要がありますあなたの問題:

constructor(angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {} 

あなたのプロバイダの前に例えばpublicを追加する必要があります

constructor(public angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {} 
+0

奇妙なことに、インジェクターでこれを公開することも必要です。私はこれをプライベートとして持っていて、公共にそれを変えてそれを固定しました。 – Bwvolleyball

1

使用providersのコンポーネントで、あなたのプロバイダを開始します。

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

これを試してください。

+1

プロバイダーブロックは必須です! – Bwvolleyball

関連する問題