填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写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