No provider for ActivatedRoute - Angular 2 RC5

Thorsten Westheider picture Thorsten Westheider · Aug 14, 2016 · Viewed 33.8k times · Source

After upgrading to Angular 2 RC5 (from RC4) it seems I can no longer inject ActivatedRoute into my components.

ORIGINAL EXCEPTION: No provider for ActivatedRoute!

Here's the relevant piece of code:

import { Component } from '@angular/core';

import { 
  ActivatedRoute
} from '@angular/router';

declare var module: {
  id: string;
};

@Component({
  moduleId: module.id,
  selector: 'mds-app',
  templateUrl: 'app.component.html',
  styleUrls: [
      'app.component.css'
  ],
  directives: []
})
export class AppComponent { 

  constructor(private _route: ActivatedRoute) {
    this._route.params.subscribe(params => console.log(_route));
  }
}

and here's my app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule }  from '@angular/common';
import { Routes, RouterModule }  from '@angular/router';

import { AppComponent }  from './app.component';

import { 
    routing, 
    appRoutingProviders 
} from './app.routing';

@NgModule({
  imports:      [ BrowserModule, CommonModule, RouterModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
  providers:    [ appRoutingProviders ]
})
export class AppModule { }

I checked the "Tour of Heroes" example and they do the exact same thing, there is no provider declaration for ActivatedRoute so what is going on here I wonder?

Answer

techguy2000 picture techguy2000 · Dec 30, 2017

If you get this error in unit tests, you need to import RouterTestingModule