Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AppInsightsService -> Router]: NullInjectorError: No provider for Router! (Angular 6) #46

Closed
Giocapetini opened this issue May 29, 2018 · 10 comments

Comments

@Giocapetini
Copy link

I am currently updating my insights service to angular 6 and after the update when i try to run ng serve i get the message

Error: StaticInjectorError(InsightsModule)[AppInsightsService -> Router]: 
  StaticInjectorError(Platform: core)[AppInsightsService -> Router]: 
    NullInjectorError: No provider for Router!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:979)
    at resolveToken (core.js:1232)
    at tryResolveToken (core.js:1182)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077)
    at resolveToken (core.js:1232)
    at tryResolveToken (core.js:1182)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1077)
    at resolveNgModuleDep (core.js:9217)
    at _createClass (core.js:9264)
    at _createProviderInstance$1 (core.js:9234)

I'm not sure on what to do... everything was working fine with angular 5.2

@MarkPieszak
Copy link
Member

Which version of the library are you using? 5.x ?

@Giocapetini
Copy link
Author

Giocapetini commented Jun 6, 2018

Sorry for it taking a while to reply Mark..
i "fixed" this error msg using
RouterModule.forRoot([ { path: '', component: AppInsightsService }, ]),
in my imports insights.module.ts
yes i was using the latest version for angular 6
(the original idea was to package the app insights in a service pre configured so that a component could be created and used in other projects by just importing the service, after i applied this and i try to bootstrap the module it gives me a new error msg.
[ERROR in Tried to find bootstrap code, but could not. Specify either statically analyzable bootstrap code or pass in an entryModule to the plugins options.])

@Giocapetini
Copy link
Author

Here's my insights.module.ts
`import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { APP_BASE_HREF } from '@angular/common';
import { ApplicationInsightsModule, AppInsightsService } from '@markpieszak/ng-application-insights';

@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot([
{ path: '', component: AppInsightsService },
]),
ApplicationInsightsModule.forRoot({
instrumentationKeySetlater: true
}),
],
providers: [
AppInsightsService,
{ provide: APP_BASE_HREF, useValue: window['_app_base'] || '/' }
],
declarations: []
})
export class InsightsModule {
}
`

HERES MY MAIN.TS

`import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { InsightsModule } from './app/insight.module';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(InsightsModule)
.catch(err => console.log(err));
`

@jondmcelroy
Copy link

I am also having the original problem.

version:

  • markpieszak/ng-application-insights: 5.0.1
  • angular: 6.0.2
  • rxjs: 6.2.1

Error: StaticInjectorError(AppModule)[AppInsightsService -> Router]:
StaticInjectorError(Platform: core)[AppInsightsService -> Router]:
NullInjectorError: No provider for Router!
at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:951)
at resolveToken (core.js:1195)
at tryResolveToken (core.js:1140)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1035)
at resolveToken (core.js:1195)
at tryResolveToken (core.js:1140)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1035)
at resolveNgModuleDep (core.js:8071)
at _createClass (core.js:8118)
at _createProviderInstance$1 (core.js:8088)

@Giocapetini
Copy link
Author

@jondmcelroy i fixed that msg with
RouterModule.forRoot([ { path: '', component: AppInsightsService }, ]),
on my app.module

@MarkPieszak
Copy link
Member

Glad you figured it out! 👍
Are we all set to close this one out?

@mikecabana
Copy link

I'm getting a similar issue. I don't think just adding RouterModule.forRoot([ { path: '', component: AppInsightsService }, ]) is the solution.

I'm not sure if anyone else has a similar issue but it should just work via your documentation.

I'm using...

  • Angular: 6.0.3
  • rxjs: 6.2.2
  • angular-application-insights: 5.0.2

Could be an issue with how Angular 6 tree shakes services now. I'll do some investigation and report back if I fins anything.

@mikecabana
Copy link

mikecabana commented Aug 17, 2018 via email

@MarkPieszak
Copy link
Member

MarkPieszak commented Sep 14, 2018

What did you need to update in the end? @mikecabana
We got a PR in that grabs the Router via the Injector so I'm wondering if that would help people here.

I'll close this one out for now as I think that might of fixed the general issue here!
If anything, open another issue just so we can make sure!

@melborp
Copy link

melborp commented Oct 12, 2018

Hi,
We use UIRouterModule and not the RouterModule in our angular application so we get the same issue.
Did the PR go through that would support using injector to get the router instead and how could we make it work with the UiRouterModule.
Thanks,
Taavi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants