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

Route

定义单个路由的配置对象。在 Routes 数组中收集了一组路由定义,以用作 Router 的配置。路由器会尝试使用此对象中定义的配置选项,将给定 URL 段与每个路由进行匹配。

A configuration object that defines a single route. A set of routes are collected in a Routes array to define a Router configuration. The router attempts to match segments of a given URL against each route, using the configuration options defined in this object.

查看"说明"...

      
      interface Route {
  path?: string
  pathMatch?: string
  matcher?: UrlMatcher
  component?: Type<any>
  redirectTo?: string
  outlet?: string
  canActivate?: any[]
  canActivateChild?: any[]
  canDeactivate?: any[]
  canLoad?: any[]
  data?: Data
  resolve?: ResolveData
  children?: Routes
  loadChildren?: LoadChildren
  runGuardsAndResolvers?: RunGuardsAndResolvers
}
    

说明

支持静态、参数化、重定向和通配符路由,以及自定义路由数据和解析方法。

Supports static, parameterized, redirect, and wildcard routes, as well as custom route data and resolve methods.

欲知详情,请参阅《路由指南》

For detailed usage information, see the Routing Guide.

Further information available in the Usage Notes...

属性

属性说明
path?: string

匹配的路径。不能与自定义 matcher 功能一起使用。使用路由器匹配表示法的 URL 字符串。可以是与任何 URL 匹配的通配符(**)(请参阅下面的使用说明)。默认值为 “/”(根路径)。

The path to match against. Cannot be used together with a custom matcher function. A URL string that uses router matching notation. Can be a wild card (**) that matches any URL (see Usage Notes below). Default is "/" (the root path).

pathMatch?: string

路径匹配策略,为 “prefix” 或 “full” 之一。默认为“prefix”。

The path-matching strategy, one of 'prefix' or 'full'. Default is 'prefix'.

默认情况下,路由器会从左边开始检查 URL 中的各个元素,以查看此 URL 是否匹配给定的路径,遇到任何一个匹配的,就停止。比如,'/team/11/user' 能匹配 'team/:id'。

By default, the router checks URL elements from the left to see if the URL matches a given path, and stops when there is a match. For example, '/team/11/user' matches 'team/:id'.

路径匹配策略 “full” 表示与整个 URL 匹配。重定向空路径路由时,执行此操作很重要。否则,因为空路径是任何 URL 的前缀,所以路由器即使在导航到重定向目标时也会进行重定向,从而造成无限循环。

The path-match strategy 'full' matches against the entire URL. It is important to do this when redirecting empty-path routes. Otherwise, because an empty path is a prefix of any URL, the router would apply the redirect even when navigating to the redirect destination, creating an endless loop.

matcher?: UrlMatcher

自定义 URL 匹配功能。不能与 path 一起使用。

A custom URL-matching function. Cannot be used together with path.

component?: Type<any>

路径匹配时实例化的组件。如果要由其子路由指定组件,则可以为空。

The component to instantiate when the path matches. Can be empty if child routes specify components.

redirectTo?: string

路径匹配时重定向到的 URL。如果 URL 以斜杠(/)开头,则为绝对值,否则相对于当前路径 URL。如果不存在,则路由器不会重定向。

A URL to redirect to when the path matches. Absolute if the URL begins with a slash (/), otherwise relative to the path URL. When not present, router does not redirect.

outlet?: string

RouterOutlet 对象的名字,当路径匹配时会把组件放置在其中。

Name of a RouterOutlet object where the component can be placed when the path matches.

canActivate?: any[]

一组依赖项注入令牌,用于查找 CanActivate() 处理器,以确定是否允许当前用户激活该组件。默认情况下,任何用户都可以激活。

An array of dependency-injection tokens used to look up CanActivate() handlers, in order to determine if the current user is allowed to activate the component. By default, any user can activate.

canActivateChild?: any[]

DI 令牌数组,用于查找 CanActivateChild() 处理器,以确定是否允许当前用户激活组件的子级。默认情况下,任何用户都可以激活子路由。

An array of DI tokens used to look up CanActivateChild() handlers, in order to determine if the current user is allowed to activate a child of the component. By default, any user can activate a child.

canDeactivate?: any[]

DI 令牌数组,用于查找 CanDeactivate() 处理器,以确定是否允许当前用户停用该组件。默认情况下,任何用户都可以停用。

An array of DI tokens used to look up CanDeactivate() handlers, in order to determine if the current user is allowed to deactivate the component. By default, any user can deactivate.

canLoad?: any[]

DI 令牌数组,用于查找 CanLoad() 处理器,以确定是否允许当前用户加载组件。默认情况下,任何用户都可以加载。

An array of DI tokens used to look up CanLoad() handlers, in order to determine if the current user is allowed to load the component. By default, any user can load.

data?: Data

ActivatedRoute 提供给组件的由开发人员定义的额外数据。默认情况下,不传递任何额外数据。

Additional developer-defined data provided to the component via ActivatedRoute. By default, no additional data is passed.

resolve?: ResolveData

DI 令牌的映射,用于查找数据解析器。请参阅 Resolve

A map of DI tokens used to look up data resolvers. See Resolve.

children?: Routes

一个子 Route 对象数组,用于指定嵌套路由配置。

An array of child Route objects that specifies a nested route configuration.

loadChildren?: LoadChildren

一个对象,指定要惰性加载的子路由。

An object specifying lazy-loaded child routes.

runGuardsAndResolvers?: RunGuardsAndResolvers

定义何时运行守卫(guard)和解析器(resolver)。为下列值之一

Defines when guards and resolvers will be run. One of

  • paramsOrQueryParamsChange :当查询参数发生变化时运行。

    paramsOrQueryParamsChange : Run when query parameters change.

  • always :在每次执行时运行。默认情况下,仅当路由的矩阵参数发生更改时,守卫和解析器才会运行。

    always : Run on every execution. By default, guards and resolvers run only when the matrix parameters of the route change.

使用说明

简单配置

Simple Configuration

以下路由指定在导航到 /team/11/user/bob 时,路由器会在其中创建带有 'User' 子组件的 'Team' 组件。

The following route specifies that when navigating to, for example, /team/11/user/bob, the router creates the 'Team' component with the 'User' child component in it.

      
      [{
  path: 'team/:id',
 component: Team,
  children: [{
    path: 'user/:name',
    component: User
  }]
}]
    

多重出口

Multiple Outlets

以下路由创建具有多个出口的兄弟组件。当导航到 /team/11(aux:chat/jim) 时,路由器会在 'Chat' 组件旁边创建 'Team' 组件。 'Chat' 组件放置在 'aux' 出口中。

The following route creates sibling components with multiple outlets. When navigating to /team/11(aux:chat/jim), the router creates the 'Team' component next to the 'Chat' component. The 'Chat' component is placed into the 'aux' outlet.

      
      [{
  path: 'team/:id',
  component: Team
}, {
  path: 'chat/:user',
  component: Chat
  outlet: 'aux'
}]
    

通配符

Wild Cards

以下路由使用通配符表示法来指定始终实例化的组件,无论你导航到何处。

The following route uses wild-card notation to specify a component that is always instantiated regardless of where you navigate to.

      
      [{
  path: '**',
  component: WildcardComponent
}]
    

重定向

Redirects

当寻找子路径时,以下路由使用 redirectTo 属性忽略给定 URL 的一部分。

The following route uses the redirectTo property to ignore a segment of a given URL when looking for a child path.

导航到 '/team/11/legacy/user/jim' 时,路由器将 URL 段 '/team/11/legacy/user/jim' 更改为 '/team/11/user/jim',然后实例化 Team 组件以及其中的 User 子组件。

When navigating to '/team/11/legacy/user/jim', the router changes the URL segment '/team/11/legacy/user/jim' to '/team/11/user/jim', and then instantiates the Team component with the User child component in it.

      
      [{
  path: 'team/:id',
  component: Team,
  children: [{
    path: 'legacy/user/:name',
    redirectTo: 'user/:name'
  }, {
    path: 'user/:name',
    component: User
  }]
}]
    

重定向路径可以是相对的(如本示例所示),也可以是绝对的。 redirectTo 值更改为绝对 URL 段 '/user/:name',则结果 URL 也是绝对 URL,'/user/jim'。

The redirect path can be relative, as shown in this example, or absolute. If we change the redirectTo value in the example to the absolute URL segment '/user/:name', the result URL is also absolute, '/user/jim'.

空路径

Empty Path

空路径路由可用来实例化一些不"消费"任何 url 区段的组件。

Empty-path route configurations can be used to instantiate components that do not 'consume' any URL segments.

在下列配置中,当导航到 /team/11 时,路由器会实例化 'AllUsers' 组件。

In the following configuration, when navigating to /team/11, the router instantiates the 'AllUsers' component.

      
      [{
  path: 'team/:id',
  component: Team,
  children: [{
    path: '',
    component: AllUsers
  }, {
    path: 'user/:name',
    component: User
  }]
}]
    

空路径路由可以有子路由。在以下示例中,当导航到 /team/11/user/jim 时,路由器会实例化带有用户组件的包装器组件。

Empty-path routes can have children. In the following example, when navigating to /team/11/user/jim, the router instantiates the wrapper component with the user component in it.

请注意,空路径路由会继承其父级的参数和数据。

Note that an empty path route inherits its parent's parameters and data.

      
      [{
  path: 'team/:id',
  component: Team,
  children: [{
    path: '',
    component: WrapperCmp,
    children: [{
      path: 'user/:name',
      component: User
    }]
  }]
}]
    

匹配策略

Matching Strategy

默认的路径匹配策略是 'prefix',这意味着路由器从左开始检查 URL 元素以查看 URL 是否与指定的路径匹配。例如,'/team/11/user' 与 'team/:id' 匹配。

The default path-match strategy is 'prefix', which means that the router checks URL elements from the left to see if the URL matches a specified path. For example, '/team/11/user' matches 'team/:id'.

      
      [{
  path: '',
  pathMatch: 'prefix', //default
  redirectTo: 'main'
}, {
  path: 'main',
  component: Main
}]
    

你可以将路径匹配策略指定为 'full',以确保路径覆盖整个未使用的 URL。重定向到空路径路由时,这样做很重要。否则,因为空路径是任何 URL 的前缀,所以路由器即使在导航到重定向目标时又会被再次重定向,从而造成无限循环。

You can specify the path-match strategy 'full' to make sure that the path covers the whole unconsumed URL. It is important to do this when redirecting empty-path routes. Otherwise, because an empty path is a prefix of any URL, the router would apply the redirect even when navigating to the redirect destination, creating an endless loop.

在以下示例中,提供 'full' pathMatch 策略可确保路由器仅在导航到 '/' 时才应用重定向。

In the following example, supplying the 'full' pathMatch strategy ensures that the router applies the redirect if and only if navigating to '/'.

      
      [{
  path: '',
  pathMatch: 'full',
  redirectTo: 'main'
}, {
  path: 'main',
  component: Main
}]
    

无组件路由

Componentless Routes

你可以在同级组件之间共享参数。例如,假设两个同级组件应该彼此相邻,并且它们两个都需要 ID 参数。你可以使用不在顶层指定组件的路由来完成此操作。

You can share parameters between sibling components. For example, suppose that two sibling components should go next to each other, and both of them require an ID parameter. You can accomplish this using a route that does not specify a component at the top level.

在以下示例中,'MainChild' 和 'AuxChild' 是同级。当导航到 'parent/10/(a//aux:b)' 时,该路由会实例化彼此相邻的主要子组件和 aux 子组件。为此,应用程序组件必须定义主要和辅助出口。

In the following example, 'MainChild' and 'AuxChild' are siblings. When navigating to 'parent/10/(a//aux:b)', the route instantiates the main child and aux child components next to each other. For this to work, the application component must have the primary and aux outlets defined.

      
      [{
   path: 'parent/:id',
   children: [
     { path: 'a', component: MainChild },
     { path: 'b', component: AuxChild, outlet: 'aux' }
   ]
}]
    

路由器将无组件父级的参数、数据和解析结果合并为子级的参数、数据和解析结果。

The router merges the parameters, data, and resolve of the componentless parent into the parameters, data, and resolve of the children.

当用空路径字符串定义子组件时,这特别有用,如以下示例所示。使用此配置,导航到 '/parent/10' 将创建主要子组件和 aux 组件。

This is especially useful when child components are defined with an empty path string, as in the following example. With this configuration, navigating to '/parent/10' creates the main child and aux components.

      
      [{
   path: 'parent/:id',
   children: [
     { path: '', component: MainChild },
     { path: '', component: AuxChild, outlet: 'aux' }
   ]
}]
    

惰性加载

Lazy Loading

惰性加载通过将应用程序拆分为多个包并按需加载它们来加快应用程序加载时间。要使用惰性加载,请在 Route 中提供 loadChildren 属性,而不是 children 属性。

Lazy loading speeds up application load time by splitting the application into multiple bundles and loading them on demand. To use lazy loading, provide the loadChildren property in the Route object, instead of the children property.

给定以下示例路由,路由器将使用浏览器原生导入体系按需惰性加载相关模块。

Given the following example route, the router will lazy load the associated module on demand using the browser native import system.

      
      [{
  path: 'lazy',
  loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule),
}];