No provider for ActivatedRoute - Angular 2 RC5

偶尔善良 提交于 2019-11-29 13:10:50

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

I was browsing through Angular 2 issues on GitHub and found the solution to the above problem out of sheer luck (see here).

I needed to add routing (see import above) to imports in NgModule, i.e.

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

Seems Angular 2's error messages just got more confusing than they already have been.

I hope this answer is of some use to someone, I was about to pull my hair out already.

EDIT: By popular request, here's a snippet for the imported routing (off the top of my head, as I'm off work this week, let me know in the comments if there are any issues with it):

app.routing.ts:

export routes: Routes = [
   { path: 'sales', component: SalesComponent }
];

export routing = RouterModule.forRoot(routes);

and in your app.module.ts you'd import this as follows:

import { routing } from 'app.routing'

I got this error during unit tests. Imported RouterTestingModule and used in my Testing module as below:

  beforeEach(
async(() => {
  TestBed.configureTestingModule({
    declarations: [],
    imports: [
      RouterTestingModule.withRoutes([{ path: 'orders/:id', component: OrdersComponent }])
    ],
    providers: []
  }).compileComponents();
})

);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!