填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

ActivatedRouteSnapshot

包含與當前元件相關的路由的當前瞬間資訊。ActivatedRoute 也可用於遍歷路由器的狀態樹。 ActivatedRouteSnapshot 也能用於遍歷路由器狀態樹。

Contains the information about a route associated with a component loaded in an outlet at a particular moment in time. ActivatedRouteSnapshot can also be used to traverse the router state tree.

檢視"說明"...

      
      class ActivatedRouteSnapshot {
  routeConfig: Route | null
  url: UrlSegment[]
  params: Params
  queryParams: Params
  fragment: string | null
  data: Data
  outlet: string
  component: Type<any> | string | null
  root: ActivatedRouteSnapshot
  parent: ActivatedRouteSnapshot | null
  firstChild: ActivatedRouteSnapshot | null
  children: ActivatedRouteSnapshot[]
  pathFromRoot: ActivatedRouteSnapshot[]
  paramMap: ParamMap
  queryParamMap: ParamMap
  toString(): string
}
    

說明

以下示例使用在建立時從根節點的快照中提取的路由資訊來初始化元件。

The following example initializes a component with route information extracted from the snapshot of the root node at the time of creation.

      
      @Component({templateUrl:'./my-component.html'})
class MyComponent {
  constructor(route: ActivatedRoute) {
    const id: string = route.snapshot.params.id;
    const url: string = route.snapshot.url.join('');
    const user = route.snapshot.data.user;
  }
}
    

建構函式

      
      constructor(url: UrlSegment[], params: Params, queryParams: Params, fragment: string, data: Data, outlet: string, component: string | Type<any>, routeConfig: Route, urlSegment: UrlSegmentGroup, lastPathIndex: number, resolve: ResolveData)
    
引數
url UrlSegment[]

與當前路由匹配的 URL 段

The URL segments matched by this route

params Params

The matrix parameters scoped to this route.

You can compute all params (or data) in the router state or to get params outside of an activated component by traversing the RouterState tree as in the following example:

      
      collectRouteParams(router: Router) {
  let params = {};
  let stack: ActivatedRouteSnapshot[] = [router.routerState.snapshot.root];
  while (stack.length > 0) {
    const route = stack.pop()!;
    params = {...params, ...route.params};
    stack.push(...route.children);
  }
  return params;
}
    
queryParams Params

所有路由共享的查詢引數(?

The query parameters shared by all the routes

fragment string

所有路由共享的 URL 片段(#

The URL fragment shared by all the routes

data Data

此路由的靜態資料和已解析資料

The static and resolved data of this route

outlet string

此路由的出口(outlet)名稱

The outlet name of the route

component string | Type

此路由對應的元件

The component of the route

routeConfig Route
urlSegment UrlSegmentGroup
lastPathIndex number
resolve ResolveData

屬性

屬性說明
routeConfig: Route | null唯讀

用於匹配當前路由的配置項

The configuration used to match this route

url: UrlSegment[]宣告在建構函式中

與當前路由匹配的 URL 段

The URL segments matched by this route

params: Params

The matrix parameters scoped to this route.

You can compute all params (or data) in the router state or to get params outside of an activated component by traversing the RouterState tree as in the following example:

      
      collectRouteParams(router: Router) {
  let params = {};
  let stack: ActivatedRouteSnapshot[] = [router.routerState.snapshot.root];
  while (stack.length > 0) {
    const route = stack.pop()!;
    params = {...params, ...route.params};
    stack.push(...route.children);
  }
  return params;
}
    
queryParams: Params宣告在建構函式中

所有路由共享的查詢引數(?

The query parameters shared by all the routes

fragment: string | null宣告在建構函式中

所有路由共享的 URL 片段(#

The URL fragment shared by all the routes

data: Data宣告在建構函式中

此路由的靜態資料和已解析資料

The static and resolved data of this route

outlet: string宣告在建構函式中

此路由的出口(outlet)名稱

The outlet name of the route

component: Type<any> | string | null宣告在建構函式中

此路由對應的元件

The component of the route

root: ActivatedRouteSnapshot唯讀

路由器狀態樹的根節點

The root of the router state

parent: ActivatedRouteSnapshot | null唯讀

在路由器狀態樹中,當前路由的父路由

The parent of this route in the router state tree

firstChild: ActivatedRouteSnapshot | null唯讀

在路由器狀態樹中,當前路由的第一個子路由

The first child of this route in the router state tree

children: ActivatedRouteSnapshot[]唯讀

在路由器狀態樹中,當前路由的所有子路由

The children of this route in the router state tree

pathFromRoot: ActivatedRouteSnapshot[]唯讀

在路由器狀態樹中從根節點開始到當前路由的完整路徑

The path from the root of the router state tree to this route

paramMap: ParamMap唯讀
queryParamMap: ParamMap唯讀

方法

      
      toString(): string
    
引數

沒有引數。

返回值

string