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>
}
参见
Routing and Navigation guide for an overview of how the
Router
service should be used.路由与导航 以获得如何使用路由器服务的概览。
说明
你可以多次导入此 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 anNgModule
that contains all the directives, the given routes, and theRouter
service itself.forChild()
方法会创建一个NgModule
,其中包含所有指令和给定的路由,但不包括Router
服务。The
forChild()
method creates anNgModule
that contains all the directives and the given routes, but does not include theRouter
service.
静态方法
带着所有路由器提供者和指令创建和配置模块。(可选)设置应用程序监听器以执行初始导航。 Creates and configures a module with all the router providers and directives. Optionally sets up an application listener to perform an initial navigation. | ||||||
参数
返回值新的
| ||||||
在根目录下注册 NgModule 时,请按以下方式导入: When registering the NgModule at the root, import as follows:
|
创建带有所有路由器指令和提供者注册的路由的模块,而无需创建新的路由器服务。注册子模块和惰性加载的子模块时,像这样创建 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: | |||
参数
返回值新的 NgModule。
| |||
|
指令
名称 | 说明 |
---|---|
| 当应用于模板中的元素时,使该元素成为开始导航到某个路由的链接。导航会在页面上的 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 |
| 跟踪元素上的链接路由当前是否处于活动状态,并允许你指定一个或多个 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. |
| 允许你在应用中链接到特定的路由。 Lets you link to specific routes in your app. |
| 一个占位符,Angular 会根据当前的路由器状态动态填充它。 Acts as a placeholder that Angular dynamically fills based on the current router state. |