NavigationStart

Represents an event triggered when a navigation starts.

代表导航开始时触发的事件。

class NavigationStart extends RouterEvent { constructor(id: number, url: string, navigationTrigger: 'imperative' | 'popstate' | 'hashchange' = 'imperative', restoredState: {...}) navigationTrigger?: 'imperative' | 'popstate' | 'hashchange' restoredState?: {...} toString(): string // 继承自 router/RouterEvent constructor(id: number, url: string) id: number url: string }

构造函数

constructor(id: number, url: string, navigationTrigger: 'imperative' | 'popstate' | 'hashchange' = 'imperative', restoredState: { navigationId: number; } | null = null)

参数

id

Type: number.

url

Type: string.

navigationTrigger

Type: 'imperative' | 'popstate' | 'hashchange'.

可选. 默认值是 'imperative'.

restoredState

Type: { navigationId: number; } | null.

可选. 默认值是 null.

属性

属性名类型说明
navigationTrigger

Identifies the trigger of the navigation.

表示导航过程中的触发器。

  • 'imperative'--triggered by router.navigateByUrl or router.navigate.

    'imperative' —— 由 router.navigateByUrlrouter.navigate 触发。

  • 'popstate'--triggered by a popstate event

    'popstate' —— 由 popstate 事件触发。

  • 'hashchange'--triggered by a hashchange event

    'hashchange' —— 由 hashchange 事件触发。

restoredState

This contains the navigation id that pushed the history record that the router navigates back to. This is not null only when the navigation is triggered by a popstate event.

这里包含的 navigationId 会被用于保存历史记录,以供浏览器导航回来。只有当这次导航是被 popstate 事件触发时,它才不为空。

The router assigns a navigationId to every router transition/navigation. Even when the user clicks on the back button in the browser, a new navigation id will be created. So from the perspective of the router, the router never "goes back". By using the restoredState and its navigationId, you can implement behavior that differentiates between creating new states and popstate events. In the latter case you can restore some remembered state (e.g., scroll position).

路由器会把一个 navigationId 赋予每一次路由器过渡/导航。即使用户点击了浏览器的后退按钮,也会创建一个新的 navigationId。 所以,从路由器的视角来看,路由器永远不会 "后退"。借助 restoredState 及其 navigationId,你可以区分开创建新状态和 popstate 事件的行为。 在 popstate 时,你可以恢复一些以前记录的状态(如滚动到的位置)。

方法

toString(): string

参数

没有参数。

返回值

string