What it does

Adds router directives and providers.

How to use

RouterModule can be imported multiple times: once per lazily-loaded bundle. Since the router deals with a global shared resource--location, we cannot have more than one router service active.

That is why there are two ways to create the module: RouterModule.forRoot and RouterModule.forChild.

  • forRoot creates a module that contains all the directives, the given routes, and the router service itself.
  • forChild creates a module that contains all the directives and the given routes, but does not include the router service.

When registered at the root, the module should be used as follows

@NgModule({ imports: [RouterModule.forRoot(ROUTES)] }) class MyNgModule {}

For submodules and lazy loaded submodules the module should be used as follows:

@NgModule({ imports: [RouterModule.forChild(ROUTES)] }) class MyNgModule {}

Class Overview

class RouterModule {
static forRoot(routes: Routes, config?: ExtraOptions) : ModuleWithProviders
static forChild(routes: Routes) : ModuleWithProviders
constructor(guard: any, router: Router)


Class Description


@NgModule({declarations: ROUTER_DIRECTIVES, exports: ROUTER_DIRECTIVES})


constructor(guard: any, router: Router)

Static Members

exported from router/index, defined in router/src/router_module.ts