660 lines
22 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
2017-04-05 02:00:40 +03:00
import {NgModuleFactory, NgModuleRef, Type} from '@angular/core';
import {Observable} from 'rxjs';
import {ActivatedRouteSnapshot} from './router_state';
import {UrlSegment, UrlSegmentGroup} from './url_tree';
/**
* Represents a route configuration for the Router service.
* An array of `Route` objects, used in `Router.config` and for nested route configurations
* in `Route.children`.
*
2021-02-04 19:45:54 +08:00
* `Route` `Router.config` `Route.children`
*
* @see `Route`
* @see `Router`
* @see [Router configuration guide](guide/router#configuration)
2021-02-04 19:45:54 +08:00
*
* [](guide/router#configuration)
*
* @publicApi
*/
export type Routes = Route[];
/**
* Represents the result of matching URLs with a custom matching function.
*
* 使 URL
2018-09-01 13:07:46 +08:00
*
* * `consumed` is an array of the consumed URL segments.
2016-07-06 16:19:52 -07:00
*
* `consumed` URL
2018-09-01 13:07:46 +08:00
*
* * `posParams` is a map of positional parameters.
2018-09-01 13:07:46 +08:00
*
* `posParams`
2018-09-01 13:07:46 +08:00
*
* @see `UrlMatcher()`
* @publicApi
*/
export type UrlMatchResult = {
consumed: UrlSegment[];
posParams?: {[name: string]: UrlSegment};
};
/**
* A function for matching a route against URLs. Implement a custom URL matcher
* for `Route.matcher` when a combination of `path` and `pathMatch`
* is not expressive enough. Cannot be used together with `path` and `pathMatch`.
2018-09-01 13:07:46 +08:00
*
* URL
* `path` `pathMatch` `Route.matcher` URL
2018-09-01 13:07:46 +08:00
*
* The function takes the following arguments and returns a `UrlMatchResult` object.
2021-02-04 19:45:54 +08:00
*
* `UrlMatchResult`
*
* * *segments* : An array of URL segments.
2021-02-04 19:45:54 +08:00
*
* *segment* URL
*
* * *group* : A segment group.
2021-02-04 19:45:54 +08:00
*
* *group*
*
* * *route* : The route to match against.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* *route*
*
* The following example implementation matches HTML files.
2018-09-01 13:07:46 +08:00
*
Merge remote-tracking branch 'en/master' into aio # Conflicts: # aio/content/cli/index.md # aio/content/guide/accessibility.md # aio/content/guide/ajs-quick-reference.md # aio/content/guide/angular-compiler-options.md # aio/content/guide/animations.md # aio/content/guide/aot-compiler.md # aio/content/guide/architecture-components.md # aio/content/guide/architecture-modules.md # aio/content/guide/architecture-next-steps.md # aio/content/guide/architecture-services.md # aio/content/guide/attribute-directives.md # aio/content/guide/bootstrapping.md # aio/content/guide/browser-support.md # aio/content/guide/cli-builder.md # aio/content/guide/comparing-observables.md # aio/content/guide/component-styles.md # aio/content/guide/creating-libraries.md # aio/content/guide/dependency-injection-in-action.md # aio/content/guide/dependency-injection-navtree.md # aio/content/guide/dependency-injection-providers.md # aio/content/guide/dependency-injection.md # aio/content/guide/deployment.md # aio/content/guide/deprecations.md # aio/content/guide/displaying-data.md # aio/content/guide/dynamic-component-loader.md # aio/content/guide/elements.md # aio/content/guide/file-structure.md # aio/content/guide/glossary.md # aio/content/guide/http.md # aio/content/guide/i18n.md # aio/content/guide/ivy-compatibility.md # aio/content/guide/language-service.md # aio/content/guide/lifecycle-hooks.md # aio/content/guide/module-types.md # aio/content/guide/ngmodule-faq.md # aio/content/guide/ngmodule-vs-jsmodule.md # aio/content/guide/ngmodules.md # aio/content/guide/observables-in-angular.md # aio/content/guide/pipes.md # aio/content/guide/providers.md # aio/content/guide/releases.md # aio/content/guide/route-animations.md # aio/content/guide/router-tutorial.md # aio/content/guide/router.md # aio/content/guide/rx-library.md # aio/content/guide/schematics-authoring.md # aio/content/guide/schematics-for-libraries.md # aio/content/guide/service-worker-config.md # aio/content/guide/service-worker-getting-started.md # aio/content/guide/structural-directives.md # aio/content/guide/styleguide.md # aio/content/guide/template-syntax.md # aio/content/guide/template-typecheck.md # aio/content/guide/testing.md # aio/content/guide/typescript-configuration.md # aio/content/guide/upgrade-setup.md # aio/content/guide/user-input.md # aio/content/guide/using-libraries.md # aio/content/guide/web-worker.md # aio/content/guide/workspace-config.md # aio/content/marketing/docs.md # aio/content/marketing/events.html # aio/content/navigation.json # aio/content/start/index.md # aio/content/start/start-data.md # aio/content/start/start-deployment.md # aio/content/tutorial/toh-pt2.md # aio/content/tutorial/toh-pt4.md # aio/package.json # aio/src/app/app.component.ts # aio/src/app/layout/footer/footer.component.html # aio/src/app/shared/custom-icon-registry.ts # aio/src/environments/environment.stable.ts # aio/tools/stackblitz-builder/builder.js # aio/tools/transforms/angular-content-package/index.js # aio/tools/transforms/templates/api/lib/memberHelpers.html # aio/yarn.lock # integration/ng_elements/e2e/app.e2e-spec.ts # integration/ng_elements/src/app.ts # packages/common/src/pipes/date_pipe.ts # packages/core/src/metadata/directives.ts # packages/core/src/metadata/ng_module.ts # packages/core/src/render/api.ts # packages/forms/src/directives/ng_model.ts # packages/forms/src/form_builder.ts # packages/forms/src/model.ts # packages/platform-browser/src/browser.ts # packages/router/src/config.ts # packages/router/src/directives/router_link.ts # packages/router/src/directives/router_link_active.ts # packages/router/src/events.ts # packages/router/src/router.ts # packages/router/src/router_module.ts # packages/router/src/router_state.ts
2020-11-28 12:50:51 +08:00
* HTML
2018-09-01 13:07:46 +08:00
*
* ```
* export function htmlFiles(url: UrlSegment[]) {
* return url.length === 1 && url[0].path.endsWith('.html') ? ({consumed: url}) : null;
* }
2018-09-01 13:07:46 +08:00
*
* export const routes = [{ matcher: htmlFiles, component: AnyComponent }];
* ```
2018-09-01 13:07:46 +08:00
*
* @publicApi
*/
export type UrlMatcher = (segments: UrlSegment[], group: UrlSegmentGroup, route: Route) =>
UrlMatchResult|null;
/**
2018-09-01 13:07:46 +08:00
*
* Represents static data associated with a particular route.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
*
*
* @see `Route#data`
2018-09-01 13:07:46 +08:00
*
* @publicApi
*/
export type Data = {
[name: string]: any
};
/**
*
* Represents the resolved data associated with a particular route.
*
2021-02-04 19:45:54 +08:00
*
*
* @see `Route#resolve`.
*
* @publicApi
*/
export type ResolveData = {
[name: string]: any
};
/**
*
* A function that is called to resolve a collection of lazy-loaded routes.
* Must be an arrow function of the following form:
* `() => import('...').then(mod => mod.MODULE)`
*
2021-02-04 19:45:54 +08:00
* `() => import('...').then(mod => mod.MODULE)`
*
* For example:
*
2021-02-04 19:45:54 +08:00
*
*
feat(router): deprecate loadChildren:string (#30073) The proposed ES dynamic import() is now supported by the Angular CLI and the larger toolchain. This renders the `loadChildren: string` API largely redundant, as import() is far more natural, is less error-prone, and is standards compliant. This commit deprecates the `string` form of `loadChildren` in favor of dynamic import(). DEPRECATION: When defining lazy-loaded route, Angular previously offered two options for configuring the module to be loaded, both via the `loadChildren` parameter of the route. Most Angular developers are familiar withthe `string` form of this API. For example, the following route definition configures Angular to load a `LazyModule` NgModule from `lazy-route/lazy.module.ts`: ``` [{ path: 'lazy', loadChildren: 'lazy-route/lazy.module#LazyModule', }] ``` This "magic string" configuration was previously necessary as there was no dynamic module loading standard on the web. This has changed with the pending standardization of dynamic `import()` expressions, which are now supported in the Angular CLI and in web tooling in general. `import()` offers a more natural and robust solution to dynamic module loading. The above example can be rewritten to use dynamic `import()`: ``` [{ path: 'lazy', loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule), }] ``` This form of lazy loading offers significant advantages in terms of: * type checking via TypeScript * simplicity of generated code * future potential to run natively in supporting browsers (see: [caniuse: dynamic import()](https://caniuse.com/#feat=es6-module-dynamic-import)) As a result, Angular is deprecating the `loadChildren: string` syntax in favor of ES dynamic `import()`. An automatic migration will run during `ng upgrade` to convert your existing Angular code to the new syntax. PR Close #30073
2019-04-23 14:30:09 -07:00
* ```
* [{
* path: 'lazy',
* loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule),
* }];
* ```
*
* @see [Route.loadChildren](api/router/Route#loadChildren)
* @publicApi
*/
export type LoadChildrenCallback = () => Type<any>|NgModuleFactory<any>|Observable<Type<any>>|
Promise<NgModuleFactory<any>|Type<any>|any>;
/**
2018-09-01 13:07:46 +08:00
*
* A function that returns a set of routes to load.
*
2021-02-04 19:45:54 +08:00
*
*
feat(router): deprecate loadChildren:string (#30073) The proposed ES dynamic import() is now supported by the Angular CLI and the larger toolchain. This renders the `loadChildren: string` API largely redundant, as import() is far more natural, is less error-prone, and is standards compliant. This commit deprecates the `string` form of `loadChildren` in favor of dynamic import(). DEPRECATION: When defining lazy-loaded route, Angular previously offered two options for configuring the module to be loaded, both via the `loadChildren` parameter of the route. Most Angular developers are familiar withthe `string` form of this API. For example, the following route definition configures Angular to load a `LazyModule` NgModule from `lazy-route/lazy.module.ts`: ``` [{ path: 'lazy', loadChildren: 'lazy-route/lazy.module#LazyModule', }] ``` This "magic string" configuration was previously necessary as there was no dynamic module loading standard on the web. This has changed with the pending standardization of dynamic `import()` expressions, which are now supported in the Angular CLI and in web tooling in general. `import()` offers a more natural and robust solution to dynamic module loading. The above example can be rewritten to use dynamic `import()`: ``` [{ path: 'lazy', loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule), }] ``` This form of lazy loading offers significant advantages in terms of: * type checking via TypeScript * simplicity of generated code * future potential to run natively in supporting browsers (see: [caniuse: dynamic import()](https://caniuse.com/#feat=es6-module-dynamic-import)) As a result, Angular is deprecating the `loadChildren: string` syntax in favor of ES dynamic `import()`. An automatic migration will run during `ng upgrade` to convert your existing Angular code to the new syntax. PR Close #30073
2019-04-23 14:30:09 -07:00
* The string form of `LoadChildren` is deprecated (see `DeprecatedLoadChildren`). The function
* form (`LoadChildrenCallback`) should be used instead.
*
2021-02-04 19:45:54 +08:00
* 使 `LoadChildren` `DeprecatedLoadChildren``LoadChildrenCallback`
*
* @see `loadChildrenCallback`
* @publicApi
*/
export type LoadChildren = LoadChildrenCallback|DeprecatedLoadChildren;
feat(router): deprecate loadChildren:string (#30073) The proposed ES dynamic import() is now supported by the Angular CLI and the larger toolchain. This renders the `loadChildren: string` API largely redundant, as import() is far more natural, is less error-prone, and is standards compliant. This commit deprecates the `string` form of `loadChildren` in favor of dynamic import(). DEPRECATION: When defining lazy-loaded route, Angular previously offered two options for configuring the module to be loaded, both via the `loadChildren` parameter of the route. Most Angular developers are familiar withthe `string` form of this API. For example, the following route definition configures Angular to load a `LazyModule` NgModule from `lazy-route/lazy.module.ts`: ``` [{ path: 'lazy', loadChildren: 'lazy-route/lazy.module#LazyModule', }] ``` This "magic string" configuration was previously necessary as there was no dynamic module loading standard on the web. This has changed with the pending standardization of dynamic `import()` expressions, which are now supported in the Angular CLI and in web tooling in general. `import()` offers a more natural and robust solution to dynamic module loading. The above example can be rewritten to use dynamic `import()`: ``` [{ path: 'lazy', loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule), }] ``` This form of lazy loading offers significant advantages in terms of: * type checking via TypeScript * simplicity of generated code * future potential to run natively in supporting browsers (see: [caniuse: dynamic import()](https://caniuse.com/#feat=es6-module-dynamic-import)) As a result, Angular is deprecating the `loadChildren: string` syntax in favor of ES dynamic `import()`. An automatic migration will run during `ng upgrade` to convert your existing Angular code to the new syntax. PR Close #30073
2019-04-23 14:30:09 -07:00
/**
* A string of the form `path/to/file#exportName` that acts as a URL for a set of routes to load.
*
2021-02-04 19:45:54 +08:00
* `path/to/file#exportName` URL
*
* @see `loadChildrenCallback`
feat(router): deprecate loadChildren:string (#30073) The proposed ES dynamic import() is now supported by the Angular CLI and the larger toolchain. This renders the `loadChildren: string` API largely redundant, as import() is far more natural, is less error-prone, and is standards compliant. This commit deprecates the `string` form of `loadChildren` in favor of dynamic import(). DEPRECATION: When defining lazy-loaded route, Angular previously offered two options for configuring the module to be loaded, both via the `loadChildren` parameter of the route. Most Angular developers are familiar withthe `string` form of this API. For example, the following route definition configures Angular to load a `LazyModule` NgModule from `lazy-route/lazy.module.ts`: ``` [{ path: 'lazy', loadChildren: 'lazy-route/lazy.module#LazyModule', }] ``` This "magic string" configuration was previously necessary as there was no dynamic module loading standard on the web. This has changed with the pending standardization of dynamic `import()` expressions, which are now supported in the Angular CLI and in web tooling in general. `import()` offers a more natural and robust solution to dynamic module loading. The above example can be rewritten to use dynamic `import()`: ``` [{ path: 'lazy', loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule), }] ``` This form of lazy loading offers significant advantages in terms of: * type checking via TypeScript * simplicity of generated code * future potential to run natively in supporting browsers (see: [caniuse: dynamic import()](https://caniuse.com/#feat=es6-module-dynamic-import)) As a result, Angular is deprecating the `loadChildren: string` syntax in favor of ES dynamic `import()`. An automatic migration will run during `ng upgrade` to convert your existing Angular code to the new syntax. PR Close #30073
2019-04-23 14:30:09 -07:00
* @publicApi
* @deprecated The `string` form of `loadChildren` is deprecated in favor of the
* `LoadChildrenCallback` function which uses the ES dynamic `import()` expression.
* This offers a more natural and standards-based mechanism to dynamically
feat(router): deprecate loadChildren:string (#30073) The proposed ES dynamic import() is now supported by the Angular CLI and the larger toolchain. This renders the `loadChildren: string` API largely redundant, as import() is far more natural, is less error-prone, and is standards compliant. This commit deprecates the `string` form of `loadChildren` in favor of dynamic import(). DEPRECATION: When defining lazy-loaded route, Angular previously offered two options for configuring the module to be loaded, both via the `loadChildren` parameter of the route. Most Angular developers are familiar withthe `string` form of this API. For example, the following route definition configures Angular to load a `LazyModule` NgModule from `lazy-route/lazy.module.ts`: ``` [{ path: 'lazy', loadChildren: 'lazy-route/lazy.module#LazyModule', }] ``` This "magic string" configuration was previously necessary as there was no dynamic module loading standard on the web. This has changed with the pending standardization of dynamic `import()` expressions, which are now supported in the Angular CLI and in web tooling in general. `import()` offers a more natural and robust solution to dynamic module loading. The above example can be rewritten to use dynamic `import()`: ``` [{ path: 'lazy', loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule), }] ``` This form of lazy loading offers significant advantages in terms of: * type checking via TypeScript * simplicity of generated code * future potential to run natively in supporting browsers (see: [caniuse: dynamic import()](https://caniuse.com/#feat=es6-module-dynamic-import)) As a result, Angular is deprecating the `loadChildren: string` syntax in favor of ES dynamic `import()`. An automatic migration will run during `ng upgrade` to convert your existing Angular code to the new syntax. PR Close #30073
2019-04-23 14:30:09 -07:00
* load an ES module at runtime.
2021-02-04 19:45:54 +08:00
*
* 使 `loadChildren` `string` 使使 ES `import()` `LoadChildrenCallback` ES
*
feat(router): deprecate loadChildren:string (#30073) The proposed ES dynamic import() is now supported by the Angular CLI and the larger toolchain. This renders the `loadChildren: string` API largely redundant, as import() is far more natural, is less error-prone, and is standards compliant. This commit deprecates the `string` form of `loadChildren` in favor of dynamic import(). DEPRECATION: When defining lazy-loaded route, Angular previously offered two options for configuring the module to be loaded, both via the `loadChildren` parameter of the route. Most Angular developers are familiar withthe `string` form of this API. For example, the following route definition configures Angular to load a `LazyModule` NgModule from `lazy-route/lazy.module.ts`: ``` [{ path: 'lazy', loadChildren: 'lazy-route/lazy.module#LazyModule', }] ``` This "magic string" configuration was previously necessary as there was no dynamic module loading standard on the web. This has changed with the pending standardization of dynamic `import()` expressions, which are now supported in the Angular CLI and in web tooling in general. `import()` offers a more natural and robust solution to dynamic module loading. The above example can be rewritten to use dynamic `import()`: ``` [{ path: 'lazy', loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule), }] ``` This form of lazy loading offers significant advantages in terms of: * type checking via TypeScript * simplicity of generated code * future potential to run natively in supporting browsers (see: [caniuse: dynamic import()](https://caniuse.com/#feat=es6-module-dynamic-import)) As a result, Angular is deprecating the `loadChildren: string` syntax in favor of ES dynamic `import()`. An automatic migration will run during `ng upgrade` to convert your existing Angular code to the new syntax. PR Close #30073
2019-04-23 14:30:09 -07:00
*/
export type DeprecatedLoadChildren = string;
/**
2018-09-01 13:07:46 +08:00
*
* How to handle query parameters in a router link.
* One of:
2021-02-04 19:45:54 +08:00
*
*
*
* - `merge` : Merge new with current parameters.
2021-02-04 19:45:54 +08:00
*
* `merge`
*
* - `preserve` : Preserve current parameters.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* `preserve`
*
* @see `UrlCreationOptions#queryParamsHandling`
* @see `RouterLink`
* @publicApi
*/
export type QueryParamsHandling = 'merge'|'preserve'|'';
/**
2018-09-01 13:07:46 +08:00
*
* A policy for when to run guards and resolvers on a route.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* guardresolver
*
* @see [Route.runGuardsAndResolvers](api/router/Route#runGuardsAndResolvers)
* @publicApi
*/
export type RunGuardsAndResolvers =
'pathParamsChange'|'pathParamsOrQueryParamsChange'|'paramsChange'|'paramsOrQueryParamsChange'|
'always'|((from: ActivatedRouteSnapshot, to: ActivatedRouteSnapshot) => boolean);
/**
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* `Routes` `Router` 使 URL
*
* Supports static, parameterized, redirect, and wildcard routes, as well as
* custom route data and resolve methods.
2016-07-06 16:19:52 -07:00
*
2021-02-04 19:45:54 +08:00
*
*
* For detailed usage information, see the [Routing Guide](guide/router).
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* [](guide/router)
*
* @usageNotes
*
2016-07-06 16:19:52 -07:00
* ### Simple Configuration
*
2018-09-01 13:07:46 +08:00
* ###
*
* 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.
*
2021-02-04 19:45:54 +08:00
* `/team/11/user/bob` 'User' 'Team'
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: 'team/:id',
2016-12-02 14:09:09 -08:00
* component: Team,
* children: [{
* path: 'user/:name',
* component: User
* }]
2016-07-06 16:19:52 -07:00
* }]
* ```
*
* ### Multiple Outlets
*
2021-02-04 19:45:54 +08:00
* ###
*
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* `/team/11(aux:chat/jim)` 'Chat' 'Team' 'Chat' 'aux'
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: 'team/:id',
* component: Team
2016-12-02 14:09:09 -08:00
* }, {
2016-07-06 16:19:52 -07:00
* path: 'chat/:user',
* component: Chat
* outlet: 'aux'
2016-07-06 16:19:52 -07:00
* }]
* ```
*
* ### Wild Cards
*
2021-02-04 19:45:54 +08:00
* ###
*
* The following route uses wild-card notation to specify a component
* that is always instantiated regardless of where you navigate to.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* 使
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: '**',
* component: WildcardComponent
2016-07-06 16:19:52 -07:00
* }]
* ```
*
* ### Redirects
*
2021-02-04 19:45:54 +08:00
* ###
*
* The following route uses the `redirectTo` property to ignore a segment of
* a given URL when looking for a child path.
*
2021-02-04 19:45:54 +08:00
* 使 `redirectTo` URL
*
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* '/team/11/legacy/user/jim' URL '/team/11/legacy/user/jim' '/team/11/user/jim' Team User
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: 'team/:id',
* component: Team,
2016-12-02 14:09:09 -08:00
* children: [{
* path: 'legacy/user/:name',
* redirectTo: 'user/:name'
* }, {
* path: 'user/:name',
* component: User
* }]
2016-07-06 16:19:52 -07:00
* }]
* ```
*
* 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'.
2021-02-04 19:45:54 +08:00
*
* `redirectTo` URL '/user/:name' URL URL'/user/jim'
*
2016-07-06 16:19:52 -07:00
* ### Empty Path
*
2018-09-01 13:07:46 +08:00
* ###
*
* Empty-path route configurations can be used to instantiate components that do not 'consume'
* any URL segments.
*
* "消费" url
*
* In the following configuration, when navigating to
* `/team/11`, the router instantiates the 'AllUsers' component.
2016-07-06 16:19:52 -07:00
*
* `/team/11` 'AllUsers'
2018-09-01 13:07:46 +08:00
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: 'team/:id',
* component: Team,
2016-12-02 14:09:09 -08:00
* children: [{
* path: '',
* component: AllUsers
* }, {
* path: 'user/:name',
* component: User
* }]
2016-07-06 16:19:52 -07:00
* }]
* ```
*
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* `/team/11/user/jim`
*
* Note that an empty path route inherits its parent's parameters and data.
2016-07-06 16:19:52 -07:00
*
2021-02-04 19:45:54 +08:00
*
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: 'team/:id',
* component: Team,
2016-12-02 14:09:09 -08:00
* children: [{
* path: '',
* component: WrapperCmp,
* children: [{
* path: 'user/:name',
* component: User
* }]
* }]
2016-07-06 16:19:52 -07:00
* }]
* ```
*
* ### Matching Strategy
*
2021-02-04 19:45:54 +08:00
* ###
*
* 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'.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* 'prefix' URL URL '/team/11/user' 'team/:id'
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: '',
* pathMatch: 'prefix', //default
* redirectTo: 'main'
2016-12-02 14:09:09 -08:00
* }, {
2016-07-06 16:19:52 -07:00
* path: 'main',
* component: Main
* }]
* ```
*
* 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.
2016-07-06 16:19:52 -07:00
*
2021-02-04 19:45:54 +08:00
* 'full'使 URL URL 使
*
* In the following example, supplying the 'full' `pathMatch` strategy ensures
* that the router applies the redirect if and only if navigating to '/'.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* 'full' `pathMatch` '/'
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: '',
* pathMatch: 'full',
* redirectTo: 'main'
2016-12-02 14:09:09 -08:00
* }, {
2016-07-06 16:19:52 -07:00
* path: 'main',
* component: Main
* }]
* ```
*
* ### Componentless Routes
*
2021-02-04 19:45:54 +08:00
* ###
*
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* ID 使
*
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* 'MainChild' 'AuxChild' 'parent/10/(a//aux:b)' aux
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
*
*
* 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.
2018-09-01 13:07:46 +08:00
*
2021-02-04 19:45:54 +08:00
* 使 '/parent/10' aux
*
2016-07-06 16:19:52 -07:00
* ```
* [{
* path: 'parent/:id',
* children: [
* { path: '', component: MainChild },
* { path: '', component: AuxChild, outlet: 'aux' }
* ]
* }]
* ```
*
* ### Lazy Loading
*
2018-09-01 13:07:46 +08:00
* ###
*
* 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.
*
2021-02-04 19:45:54 +08:00
* 使 `Route` `loadChildren` `children`
*
* Given the following example route, the router will lazy load
* the associated module on demand using the browser native import system.
*
2021-02-04 19:45:54 +08:00
* 使
*
* ```
* [{
* path: 'lazy',
* loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule),
* }];
* ```
*
* @publicApi
*/
2016-05-23 16:14:23 -07:00
export interface Route {
/**
* 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).
*
2021-02-04 19:45:54 +08:00
* `matcher` 使使 URL URL `**`使 /
*
*/
2016-05-23 16:14:23 -07:00
path?: string;
/**
* The path-matching strategy, one of 'prefix' or 'full'.
* Default is 'prefix'.
*
2021-02-04 19:45:54 +08:00
* prefix full prefix
*
* 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'.
*
2021-02-04 19:45:54 +08:00
* URL URL '/team/11/user' 'team/:id'
*
* 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.
*
2021-02-04 19:45:54 +08:00
* full URL URL 使
*
*/
pathMatch?: string;
/**
* A custom URL-matching function. Cannot be used together with `path`.
2021-02-04 19:45:54 +08:00
*
* URL `path` 使
*
*/
matcher?: UrlMatcher;
/**
* The component to instantiate when the path matches.
* Can be empty if child routes specify components.
2021-02-04 19:45:54 +08:00
*
*
*
*/
component?: Type<any>;
/**
* 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.
2021-02-04 19:45:54 +08:00
*
* URL URL / URL
*
*/
2016-06-28 14:49:29 -07:00
redirectTo?: string;
/**
* Name of a `RouterOutlet` object where the component can be placed
* when the path matches.
2021-02-04 19:45:54 +08:00
*
* `RouterOutlet`
*/
2016-05-23 16:14:23 -07:00
outlet?: string;
/**
* 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.
2021-02-04 19:45:54 +08:00
*
* `CanActivate()`
*
*/
2016-06-07 09:50:35 -07:00
canActivate?: any[];
/**
* 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.
2021-02-04 19:45:54 +08:00
*
* DI `CanActivateChild()`
*
*/
canActivateChild?: any[];
/**
* 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.
*
2021-02-04 19:45:54 +08:00
* DI `CanDeactivate()`
*
*/
2016-06-07 09:50:35 -07:00
canDeactivate?: any[];
/**
* 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.
2021-02-04 19:45:54 +08:00
*
* DI `CanLoad()`
*
*/
2016-07-26 14:39:02 -07:00
canLoad?: any[];
/**
* Additional developer-defined data provided to the component via
* `ActivatedRoute`. By default, no additional data is passed.
2021-02-04 19:45:54 +08:00
*
* `ActivatedRoute`
*
*/
data?: Data;
/**
* A map of DI tokens used to look up data resolvers. See `Resolve`.
2021-02-04 19:45:54 +08:00
*
* DI `Resolve`
*
*/
resolve?: ResolveData;
/**
* An array of child `Route` objects that specifies a nested route
* configuration.
2021-02-04 19:45:54 +08:00
*
* `Route`
*
*/
children?: Routes;
/**
* An object specifying lazy-loaded child routes.
2021-02-04 19:45:54 +08:00
*
*
*
*/
loadChildren?: LoadChildren;
/**
* Defines when guards and resolvers will be run. One of
2021-02-04 19:45:54 +08:00
*
* guardresolver
*
* - `paramsOrQueryParamsChange` : Run when query parameters change.
2021-02-04 19:45:54 +08:00
*
* `paramsOrQueryParamsChange`
*
* - `always` : Run on every execution.
* By default, guards and resolvers run only when the matrix
* parameters of the route change.
2021-02-04 19:45:54 +08:00
*
* `always`
*/
runGuardsAndResolvers?: RunGuardsAndResolvers;
/**
* Filled for routes with `loadChildren` once the module has been loaded
2021-02-04 19:45:54 +08:00
*
* 使 `loadChildren`
*
* @internal
*/
2017-04-05 02:00:40 +03:00
_loadedConfig?: LoadedRouterConfig;
}
export class LoadedRouterConfig {
constructor(public routes: Route[], public module: NgModuleRef<any>) {}
}