Router
提供导航和操纵 URL 的能力。
Provides the navigation and url manipulation capabilities.
class Router {
constructor(rootComponentType: Type<any>, urlSerializer: UrlSerializer, rootContexts: ChildrenOutletContexts, location: Location, injector: Injector, loader: NgModuleFactoryLoader, compiler: Compiler, config: Route[])
events: Observable<Event>
routerState: RouterState
errorHandler: ErrorHandler
malformedUriErrorHandler: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree
navigated: boolean
urlHandlingStrategy: UrlHandlingStrategy
routeReuseStrategy: RouteReuseStrategy
onSameUrlNavigation: 'reload' | 'ignore'
paramsInheritanceStrategy: 'emptyOnly' | 'always'
urlUpdateStrategy: 'deferred' | 'eager'
relativeLinkResolution: 'legacy' | 'corrected'
config: Routes
url: string
initialNavigation(): void
setUpLocationChangeListener(): void
getCurrentNavigation(): Navigation | null
resetConfig(config: Route[]): void
ngOnDestroy(): void
dispose(): void
createUrlTree(commands: any[], navigationExtras: NavigationExtras = {}): UrlTree
navigateByUrl(url: string | UrlTree, extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean>
navigate(commands: any[], extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean>
serializeUrl(url: UrlTree): string
parseUrl(url: string): UrlTree
isActive(url: string | UrlTree, exact: boolean): boolean
}
说明
查看 Routes
以了解详情和范例。
See Routes
for more details and examples.
构造函数
创建路由器服务。 Creates the router service. | ||||||||||||||||||||||||
参数
|
属性
属性 | 说明 |
---|---|
events: Observable<Event> | 只读 |
routerState: RouterState | 只读 |
errorHandler: ErrorHandler | 当导航发生错误时要调用的错误处理器。 Error handler that is invoked when a navigation errors. 欲知详情,参见 See |
malformedUriErrorHandler: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree | uri 格式无效错误的处理器,在 Malformed uri error handler is invoked when |
navigated: boolean | 表示是否发生过至少一次导航。 Indicates if at least one navigation happened. |
urlHandlingStrategy: UrlHandlingStrategy | 提取并合并 URL。在 AngularJS 向 Angular 迁移时会用到。 Extracts and merges URLs. Used for AngularJS to Angular migrations. |
routeReuseStrategy: RouteReuseStrategy | |
onSameUrlNavigation: 'reload' | 'ignore' | 定义当路由器收到一个导航到当前 URL 的请求时应该怎么做。 默认情况下,路由器将会忽略这次导航。但这样会阻止类似于 "刷新" 按钮的特性。 使用该选项可以配置导航到当前 URL 时的行为。默认值为 'ignore'。 Define what the router should do if it receives a navigation request to the current URL. By default, the router will ignore this navigation. However, this prevents features such as a "refresh" button. Use this option to configure the behavior when navigating to the current URL. Default is 'ignore'. |
paramsInheritanceStrategy: 'emptyOnly' | 'always' | 定义路由器如何从父路由向子路由合并参数、数据。可用选项为: Defines how the router merges params, data and resolved data from parent to child routes. Available options are:
|
urlUpdateStrategy: 'deferred' | 'eager' | 定义路由器要何时更新浏览器的 URL。默认行为是在每次成功的导航之后更新。 不过,有些应用会更愿意在导航开始时就更新。最常见的情况是尽早更新 URL,这样当导航失败时,你就可以在出错的 URL 上显示一条错误信息了。 可用的选项包括: Defines when the router updates the browser URL. The default behavior is to update after successful navigation. However, some applications may prefer a mode where the URL gets updated at the beginning of navigation. The most common use case would be updating the URL early so if navigation fails, you can show an error message with the URL that failed. Available options are:
|
relativeLinkResolution: 'legacy' | 'corrected' | 欲知详情,参见 See |
config: Routes | 声明于构造函数中 |
url: string | 只读 当前 URL The current url |
方法
设置位置变化监听器,并执行首次导航。 Sets up the location change listener and performs the initial navigation. |
设置位置变化监听器。 Sets up the location change listener. |
参数没有参数。 返回值
|
The current Navigation object if one exists |
重置供导航和生成链接使用的配置项。 Resets the configuration used for navigation and generating links. |
使用说明用法Example
|
参数没有参数。 返回值
|
销毁路由器 Disposes of the router |
参数没有参数。 返回值
|
把一个命令数组应用于当前的 URL 树,并创建一个新的 URL 树。 Applies an array of commands to the current url tree and creates a new url tree. | ||||||
参数
返回值 | ||||||
如果指定了激活路由,就以该路由为起点应用这些命令。 如果没有指定激活路由,就以根路由为起点应用这些命令。 When given an activate route, applies the given commands starting from the route. When not given a route, applies the given command starting from the root. | ||||||
使用说明用法Example
|
基于所提供的 url 进行导航。这种导航永远使用绝对路径。 Navigate based on the provided url. This navigation is always absolute. | ||||||
参数
返回值
| ||||||
返回一个 Promise: Returns a promise that:
| ||||||
使用说明用法Example
由于 Since |
基于所提供的命令数组和起点路由进行导航。 如果没有指定起点路由,则从根路由开始进行绝对导航。 Navigate based on the provided array of commands and a starting point. If no starting route is provided, the navigation is absolute. | ||||||
参数
返回值
| ||||||
返回一个 Promise: Returns a promise that:
| ||||||
使用说明用法Example
The first parameter of 要想影响到浏览器的 In order to affect this browser's |
把 Serializes a |
把字符串解析为 Parses a string into a |
返回指定的 Returns whether the url is activated |