填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

ActivatedRoute

允许访问与某出口中加载的组件关联路由信息。用于遍历 RouterState 树并从节点提取信息。

Provides access to information about a route associated with a component that is loaded in an outlet. Use to traverse the RouterState tree and extract information from nodes.

查看"说明"...

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

参见

说明

以下示例演示如何使用当前激活的路由中的信息来构造组件。

The following example shows how to construct a component using information from a currently activated route.

activated-route.component.ts
      
      import {Component} from '@angular/core';
/* . . . */
import {ActivatedRoute} from '@angular/router';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
/* . . . */

@Component({
/* . . . */
})
export class ActivatedRouteComponent {
  constructor(route: ActivatedRoute) {
    const id: Observable<string> = route.params.pipe(map(p => p.id));
    const url: Observable<string> = route.url.pipe(map(segments => segments.join('')));
    // route.data includes both `data` and `resolve`
    const user = route.data.pipe(map(d => d.user));
  }
}
    

构造函数

      
      constructor(url: Observable<UrlSegment[]>, params: Observable<Params>, queryParams: Observable<Params>, fragment: Observable<string>, data: Observable<Data>, outlet: string, component: string | Type<any>, futureSnapshot: ActivatedRouteSnapshot)
    
参数
url Observable

一个 Observable,表示与当前路由匹配的 URL 段。

An observable of the URL segments matched by this route.

params Observable

一个 Observable,表示当前路由范围内的矩阵参数(;)。

An observable of the matrix parameters scoped to this route.

queryParams Observable

一个 Observable,表示所有路由共享的查询参数(?)。

An observable of the query parameters shared by all the routes.

fragment Observable

一个 Observable,表示由所有路由共享的 URL 片段(#)。

An observable of the URL fragment shared by all the routes.

data Observable

一个 Observable,表示该路由的静态数据和解析出的数据。

An observable of the static and resolved data of this route.

outlet string

本路由对应的出口名,是个常量

The outlet name of the route. It's a constant

component string | Type

本路由对应的组件,是个常量

The component of the route. It's a constant

futureSnapshot ActivatedRouteSnapshot

属性

属性说明
snapshot: ActivatedRouteSnapshot

本路由此刻的快照

The current snapshot of this route

url: Observable<UrlSegment[]>声明在构造函数中

一个 Observable,表示与当前路由匹配的 URL 段。

An observable of the URL segments matched by this route.

params: Observable<Params>声明在构造函数中

一个 Observable,表示当前路由范围内的矩阵参数(;)。

An observable of the matrix parameters scoped to this route.

queryParams: Observable<Params>声明在构造函数中

一个 Observable,表示所有路由共享的查询参数(?)。

An observable of the query parameters shared by all the routes.

fragment: Observable<string | null>声明在构造函数中

一个 Observable,表示由所有路由共享的 URL 片段(#)。

An observable of the URL fragment shared by all the routes.

data: Observable<Data>声明在构造函数中

一个 Observable,表示该路由的静态数据和解析出的数据。

An observable of the static and resolved data of this route.

outlet: string声明在构造函数中

本路由对应的出口名,是个常量

The outlet name of the route. It's a constant

component: Type<any> | string | null声明在构造函数中

本路由对应的组件,是个常量

The component of the route. It's a constant

routeConfig: Route | null只读

用于匹配本路由的配置项。

The configuration used to match this route.

root: ActivatedRoute只读

路由器状态树的根节点。

The root of the router state.

parent: ActivatedRoute | null只读

在路由器状态树中,当前路由的父路由。

The parent of this route in the router state tree.

firstChild: ActivatedRoute | null只读

在路由器状态树中,当前路由的第一个子路由。

The first child of this route in the router state tree.

children: ActivatedRoute[]只读

在路由器状态树中,当前路由的所有子路由。

The children of this route in the router state tree.

pathFromRoot: ActivatedRoute[]只读

在路由器状态树中从根节点开始到当前路由的完整路径。

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

paramMap: Observable<ParamMap>只读

一个 Observable,其中包含特定于路由的必要和可选参数的映射。该映射支持从同一参数中检索单个和多个值。

An Observable that contains a map of the required and optional parameters specific to the route. The map supports retrieving single and multiple values from the same parameter.

queryParamMap: Observable<ParamMap>只读

一个 Observable,其中包含可用于所有路由的查询参数的映射。该映射支持从查询参数中检索单个和多个值。

An Observable that contains a map of the query parameters available to all routes. The map supports retrieving single and multiple values from the query parameter.

方法

      
      toString(): string
    
参数

没有参数。

返回值

string