填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

RouterModule

添加指令和提供者,以便在应用程序中定义的视图之间进行应用内导航。使用 Angular Router 服务以声明方式指定应用程序状态并管理状态转换。

Adds directives and providers for in-app navigation among views defined in an application. Use the Angular Router service to declaratively specify application states and manage state transitions.

查看"说明"...

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

参见

说明

你可以多次导入此 NgModule,对于每个惰性加载的包导入一次。但是,只能有一个 Router 服务是活动的。为确保这一点,在导入此模块时有两种方法来注册路由:

You can import this NgModule multiple times, once for each lazy-loaded bundle. However, only one Router service can be active. To ensure this, there are two ways to register routes when importing this module:

  • forRoot() 方法会创建一个 NgModule,其中包含所有指令、给定的路由以及 Router 服务本身。

    The forRoot() method creates an NgModule that contains all the directives, the given routes, and the Router service itself.

  • forChild() 方法会创建一个 NgModule,其中包含所有指令和给定的路由,但不包括 Router 服务。

    The forChild() method creates an NgModule that contains all the directives and the given routes, but does not include the Router service.

静态方法

带着所有路由器提供者和指令创建和配置模块。(可选)设置应用程序监听器以执行初始导航。

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

      
      static forRoot(routes: Route[], config?: ExtraOptions): ModuleWithProviders<RouterModule>
    
参数
routes Route[]

Route 对象的数组,这些对象定义应用程序的导航路径。

An array of Route objects that define the navigation paths for the application.

config ExtraOptions

一个 ExtraOptions 配置对象,该对象会控制如何执行导航。

An ExtraOptions configuration object that controls how navigation is performed.

可选. 默认值是 `undefined`.
返回值

新的 NgModule

ModuleWithProviders<RouterModule>: The new NgModule.

在根目录下注册 NgModule 时,请按以下方式导入:

When registering the NgModule at the root, import as follows:

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

创建带有所有路由器指令和提供者注册的路由的模块,而无需创建新的路由器服务。注册子模块和惰性加载的子模块时,像这样创建 NgModule:

Creates a module with all the router directives and a provider registering routes, without creating a new Router service. When registering for submodules and lazy-loaded submodules, create the NgModule as follows:

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

Route 对象的数组,它们定义了子模块的导航路径。

An array of Route objects that define the navigation paths for the submodule.

返回值

新的 NgModule。

ModuleWithProviders<RouterModule>: The new NgModule.

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

指令

名称说明
      
      RouterLink
    

当应用于模板中的元素时,使该元素成为开始导航到某个路由的链接。导航会在页面上的 <router-outlet> 位置上打开一个或多个路由组件。

When applied to an element in a template, makes that element a link that initiates navigation to a route. Navigation opens one or more routed components in one or more <router-outlet> locations on the page.

      
      RouterLinkActive
    

跟踪元素上的链接路由当前是否处于活动状态,并允许你指定一个或多个 CSS 类,以便在链接路由处于活动状态时添加到该元素。

Tracks whether the linked route of an element is currently active, and allows you to specify one or more CSS classes to add to the element when the linked route is active.

      
      RouterLinkWithHref
    

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

Lets you link to specific routes in your app.

      
      RouterOutlet
    

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

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