最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 : EXCEPTION: No provider for Angulartics2GoogleAnalytics - Stack Overflow

programmeradmin2浏览0评论

I am trying to use angulartics2 with my application.

I have configured correctly as it mentioned in the document.

Note: There is no place it is mentioned to add the provider as a dependency..

When i try to run the application, it shows the error as below.

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:

Appponent.ts

import { Component } from '@angular/core';
import { Angulartics2GoogleAnalytics } from 'angulartics2';
@Component({
  selector: 'my-app',
  moduleId: module.id,
  templateUrl: `./appponent.html`,
  styleUrls: ['/appponenet.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 './appponent';
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 { }

I am trying to use angulartics2 with my application.

I have configured correctly as it mentioned in the document.

Note: There is no place it is mentioned to add the provider as a dependency..

When i try to run the application, it shows the error as below.

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./angulartics/angulartics2

App.ponent.ts

import { Component } from '@angular/core';
import { Angulartics2GoogleAnalytics } from 'angulartics2';
@Component({
  selector: 'my-app',
  moduleId: module.id,
  templateUrl: `./app.ponent.html`,
  styleUrls: ['/app.ponenet.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.ponent';
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 { }
Share Improve this question edited Jan 29, 2017 at 8:15 Sajeetharan asked Jan 29, 2017 at 7:53 SajeetharanSajeetharan 223k65 gold badges366 silver badges408 bronze badges 4
  • 1 Possible duplicate of Angular 2 - No Provider error (even though I've added a provider) – Estus Flask Commented Jan 29, 2017 at 8:23
  • Yes, it should be done like you've done. This may happen if the modules became duplicated, and Angulartics2GoogleAnalytics refers to different classes in different modules. You have App.ponent.ts but import ./app.ponent, this may indicate that you were careless with case, and it would lead to module duplication. I would suggest to check if Angulartics2GoogleAnalytics is really the same class in all modules, as it were suggested in the answer in dupe question. – Estus Flask Commented Jan 29, 2017 at 8:27
  • @estus No it dint work ! i have tried for 2 hours – Sajeetharan Commented Jan 29, 2017 at 12:42
  • To make sure, by 'it'` you mean that you tried to expose Angulartics2GoogleAnalytics as window property in both classes, and it was === equal when you pared it? If you can't provide MCVE that can replicate the issue, I don't think that anything can be suggested as an answer here. – Estus Flask Commented Jan 29, 2017 at 13:11
Add a ment  | 

2 Answers 2

Reset to default 4

use providers to initiate your providers in ponent.

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

Try this.

Your problem should reside in your constructor:

constructor(angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {}

you should add e.g public in front of your provider:

constructor(public angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics) {}
发布评论

评论列表(0)

  1. 暂无评论