ExtraOptions
表示路由器的配置项。
A set of configuration options for a router module, provided in the forRoot()
method.
interface ExtraOptions {
enableTracing?: boolean
useHash?: boolean
initialNavigation?: InitialNavigation
errorHandler?: ErrorHandler
preloadingStrategy?: any
onSameUrlNavigation?: 'reload' | 'ignore'
scrollPositionRestoration?: 'disabled' | 'enabled' | 'top'
scrollOffset?: [number, number] | (() => [number, number])
paramsInheritanceStrategy?: 'emptyOnly' | 'always'
malformedUriErrorHandler?: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree
urlUpdateStrategy?: 'deferred' | 'eager'
relativeLinkResolution?: 'legacy' | 'corrected'
}
属性
属性 | 说明 |
---|---|
enableTracing?: boolean | When true, log all internal navigation events to the console. Use for debugging. |
useHash?: boolean | 修改位置策略( When true, enable the location strategy that uses the URL fragment instead of the history API. |
initialNavigation?: InitialNavigation | One of Legacy values are deprecated since v4 and should not be used for new applications:
禁用首次导航 |
errorHandler?: ErrorHandler | 自定义的错误处理器。 A custom error handler for failed navigations. |
preloadingStrategy?: any | 配置预加载策略,参见 Configures a preloading strategy. One of |
onSameUrlNavigation?: 'reload' | 'ignore' | 规定当路由器收到一个导航到当前 URL 的请求时该如何处理。 默认情况下,路由器会忽略本次导航。不过,这会阻止实现类似于"刷新"按钮的功能。 使用该选项可以控制导航到当前 URL 时的行为。默认为 'ignore'。 Define what the router should do if it receives a navigation request to the current URL. Default is |
scrollPositionRestoration?: 'disabled' | 'enabled' | 'top' | 配置是否需要在导航回来的时候恢复滚动位置。 Configures if the scroll position needs to be restored when navigating back.
你可以像下面的例子一样适配它启用时的行为,来自定义恢复滚动位置的策略: You can implement custom scroll restoration behavior by adapting the enabled behavior as in the following example.
|
scrollOffset?: [number, number] | (() => [number, number]) | When set to 'enabled', scrolls to the anchor element when the URL has a fragment. Anchor scrolling is disabled by default. Anchor scrolling does not happen on 'popstate'. Instead, we restore the position that we stored or scroll to the top. anchorScrolling?: 'disabled'|'enabled'; 配置当滚动到一个元素时,路由器使用的滚动偏移。 /** Configures the scroll offset the router will use when scrolling to an element. 当给出两个数字时,路由器总会使用它们。 当给出一个函数时,路由器每当要恢复滚动位置时,都会调用该函数。 When given a tuple with x and y position value, the router uses that offset each time it scrolls. When given a function, the router invokes the function every time it restores scroll position. |
paramsInheritanceStrategy?: 'emptyOnly' | 'always' | Defines how the router merges parameters, data, and resolved data from parent to child routes. By default ('emptyOnly'), inherits parent parameters only for path-less or component-less routes. Set to 'always' to enable unconditional inheritance of parent parameters. |
malformedUriErrorHandler?: (error: URIError, urlSerializer: UrlSerializer, url: string) => UrlTree | 一个自定义的 URI 格式无效错误的处理器。每当 encodeURI 包含无效字符序列时,就会调用该处理器。默认的实现是跳转到根路径,抛弃任何路径和参数信息。该函数传入三个参数: A custom handler for malformed URI errors. The handler is invoked when
|
urlUpdateStrategy?: 'deferred' | 'eager' | Defines when the router updates the browser URL. By default ('deferred'), update after successful navigation. Set to 'eager' if prefer to update the URL at the beginning of navigation. Updating the URL early allows you to handle a failure of navigation by showing an error message with the URL that failed. |
relativeLinkResolution?: 'legacy' | 'corrected' | 启用 BUG 补丁,纠正空路径组件的相对链接解析问题。 Enables a bug fix that corrects relative link resolution in components with empty paths. Example:
在 From the
不过,可以这样用: However, this will work:
换句话说,要使用 In other words, you're required to use |