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

RouterModule

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: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule>
  static forChild(routes: Routes): ModuleWithProviders<RouterModule>
}
    

参见

说明

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:

  • The forRoot() method creates an NgModule that contains all the directives, the given routes, and the Router service itself.
  • 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: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule>
      
      static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule>
    
参数
routes Routes

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

config ExtraOptions

可选. 默认值是 undefined.

An ExtraOptions configuration object that controls how navigation is performed.

返回值

ModuleWithProviders<RouterModule>: The new NgModule.

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

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

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: Routes): ModuleWithProviders<RouterModule>
      
      static forChild(routes: Routes): ModuleWithProviders<RouterModule>
    
参数
routes Routes

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

返回值

ModuleWithProviders<RouterModule>: The new NgModule.

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

指令

名称说明
RouterLink
      
      RouterLink
    

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
      
      RouterLinkActive
    

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