【发布时间】: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