填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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.