RouterModule

Adds router directives and providers.

添加路由器指令和服务提供商。

See more...

class RouterModule { static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule> static forChild(routes: Routes): ModuleWithProviders<RouterModule> }

说明

Managing state transitions is one of the hardest parts of building applications. This is especially true on the web, where you also need to ensure that the state is reflected in the URL. In addition, we often want to split applications into multiple bundles and load them on demand. Doing this transparently is not trivial.

在构建应用时,管理状态的转换是最难的任务之一。对 Web 来说尤其如此,你还要确保这个状态同时在 URL 中反映出来。 另外,我们通常会希望把应用拆分成多个发布包,并按需加载。要让这些工作透明化,可没那么简单。

The Angular router solves these problems. Using the router, you can declaratively specify application states, manage state transitions while taking care of the URL, and load bundles on demand.

Angular 的路由器解决了这些问题。使用路由器,你可以声明式的指定应用的状态、管理状态的转换,还可以处理好 URL,还可以按需加载发布包。

Read this developer guide to get an overview of how the router should be used.

阅读开发指南 以获得如何使用路由器的全景图。

Static methods

Creates a module with all the router providers and directives. It also optionally sets up an application listener to perform an initial navigation.

创建一个带有所有路由器服务提供商和指令的模块。它还可以(可选的)设置一个应用监听器,来执行首次导航。

static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule>

参数

routes

Type: Routes.

config

Type: ExtraOptions.

可选. 默认值是 undefined.

返回值

ModuleWithProviders<RouterModule>

Options (see ExtraOptions):

选项(参见 ExtraOptions):

  • enableTracing makes the router log all its internal events to the console.

    enableTracing 让路由器把它所有的内部事件都记录到控制台中。

  • useHash enables the location strategy that uses the URL fragment instead of the history API.

    useHash 修改位置策略(LocationStrategy),用 URL 片段(#)代替 history API。

  • initialNavigation disables the initial navigation.

    initialNavigation 禁用首次导航。

  • errorHandler provides a custom error handler.

    errorHandler 提供一个自定义的错误处理器。

  • preloadingStrategy configures a preloading strategy (see PreloadAllModules).

    preloadingStrategy 配置预加载策略(参见 PreloadAllModules)。

  • onSameUrlNavigation configures how the router handles navigation to the current URL. See ExtraOptions for more details.

    onSameUrlNavigation 会配置路由器在导航到当前 URL 时该如何处理。欲知详情,参见 ExtraOptions

  • paramsInheritanceStrategy defines how the router merges params, data and resolved data from parent to child routes.

    paramsInheritanceStrategy 定义了路由器要如何把父路由的参数、数据和解析出的数据合并到子路由中。

Creates a module with all the router directives and a provider registering routes.

创建一个具有所有路由器指令和一个用于注册路由的提供商。

static forChild(routes: Routes): ModuleWithProviders<RouterModule>

参数

routes

Type: Routes.

返回值

ModuleWithProviders<RouterModule>

Directives

NameDescription
RouterLink

Lets you link to specific routes in your app.

让你可以在应用中链接到特定的路由。

RouterLinkActive

Lets you add a CSS class to an element when the link's route becomes active.

当此链接指向的路由激活时,往宿主元素上添加一个 CSS 类。

RouterLinkWithHref

Lets you link to specific routes in your app.

允许你在应用中链接到特定的路由。

RouterOutlet

Acts as a placeholder that Angular dynamically fills based on the current router state.

一个占位符,Angular 会根据当前的路由器状态动态填充它。

使用说明

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.

RouterModule 可能会被多次导入:每个惰性加载的发布包都会导入一次。 但由于路由器要和全局共享的资源 - location 打交道,所以不能同时激活一个以上的 Router 服务。

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

这就是需要两种方式来创建本模块的原因:RouterModule.forRootRouterModule.forChild

  • forRoot creates a module that contains all the directives, the given routes, and the router service itself.

    forRoot 创建一个包含所有指令、指定的路由和 Router 服务本身的模块。

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

    forChild 会创建一个包含所有指令、指定的路由,但不含 Router 服务的模块。

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 {}