RouterModule

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

Adds router directives and providers.

查看"说明"...

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

说明

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

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.

Angular 的路由器解决了这些问题。使用路由器,你可以声明式的指定应用的状态、管理状态的转换,还可以处理好 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.

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

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

静态方法

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

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: Route[], config?: ExtraOptions): ModuleWithProviders<RouterModule>
      
      static forRoot(routes: Route[], config?: ExtraOptions): ModuleWithProviders<RouterModule>
    
参数
routes Route[]
config ExtraOptions

可选. 默认值是 undefined.

返回值

ModuleWithProviders<RouterModule>

选项(参见 ExtraOptions):

Options (see ExtraOptions):

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

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

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

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

  • initialNavigation 禁用首次导航。

    initialNavigation disables the initial navigation.

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

    errorHandler provides a custom error handler.

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

    preloadingStrategy configures a preloading strategy (see PreloadAllModules).

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

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

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

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

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

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

static forChild(routes: Route[]): ModuleWithProviders<RouterModule>
      
      static forChild(routes: Route[]): ModuleWithProviders<RouterModule>
    
参数
routes Route[]
返回值

ModuleWithProviders<RouterModule>

指令

名称说明
RouterLink
      
      RouterLink
    

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

Lets you link to specific routes in your app.

RouterLinkActive
      
      RouterLinkActive
    

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

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

RouterLinkWithHref
      
      RouterLinkWithHref
    

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

Lets you link to specific routes in your app.

RouterOutlet
      
      RouterOutlet
    

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

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

使用说明

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

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.forRootRouterModule.forChild

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

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

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

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

    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 {}
      
      @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 {}
      
      @NgModule({
  imports: [RouterModule.forChild(ROUTES)]
})
class MyNgModule {}