2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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-04 19:00:40 -04:00
|
|
|
import {NgModuleFactory, NgModuleRef, Type} from '@angular/core';
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Observable} from 'rxjs';
|
2018-12-14 14:02:50 -05:00
|
|
|
|
2018-04-06 18:56:36 -04:00
|
|
|
import {EmptyOutletComponent} from './components/empty_outlet';
|
2018-12-14 14:02:50 -05:00
|
|
|
import {ActivatedRouteSnapshot} from './router_state';
|
2016-12-02 17:09:09 -05:00
|
|
|
import {PRIMARY_OUTLET} from './shared';
|
2016-11-09 18:25:47 -05:00
|
|
|
import {UrlSegment, UrlSegmentGroup} from './url_tree';
|
2016-08-19 18:48:09 -04:00
|
|
|
|
2018-12-14 14:02:50 -05:00
|
|
|
|
2016-06-27 15:27:23 -04:00
|
|
|
/**
|
2018-05-29 15:25:26 -04:00
|
|
|
* 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`.
|
|
|
|
*
|
|
|
|
* @see `Route`
|
|
|
|
* @see `Router`
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
|
|
|
export type Routes = Route[];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the result of matching URLs with a custom matching function.
|
|
|
|
*
|
|
|
|
* * `consumed` is an array of the consumed URL segments.
|
|
|
|
* * `posParams` is a map of positional parameters.
|
|
|
|
*
|
|
|
|
* @see `UrlMatcher()`
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
|
|
|
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`
|
2019-10-02 13:54:57 -04:00
|
|
|
* is not expressive enough. Cannot be used together with `path` and `pathMatch`.
|
2018-05-29 15:25:26 -04:00
|
|
|
*
|
|
|
|
* @param segments An array of URL segments.
|
|
|
|
* @param group A segment group.
|
|
|
|
* @param route The route to match against.
|
2019-10-02 13:54:57 -04:00
|
|
|
* @returns The match-result.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
2018-09-20 09:50:32 -04:00
|
|
|
* @usageNotes
|
2018-05-29 15:25:26 -04:00
|
|
|
*
|
|
|
|
* The following matcher matches HTML files.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* export function htmlFiles(url: UrlSegment[]) {
|
|
|
|
* return url.length === 1 && url[0].path.endsWith('.html') ? ({consumed: url}) : null;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* export const routes = [{ matcher: htmlFiles, component: AnyComponent }];
|
|
|
|
* ```
|
|
|
|
*
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
|
|
|
export type UrlMatcher = (segments: UrlSegment[], group: UrlSegmentGroup, route: Route) =>
|
|
|
|
UrlMatchResult;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Represents static data associated with a particular route.
|
|
|
|
*
|
|
|
|
* @see `Route#data`
|
|
|
|
*
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
|
|
|
export type Data = {
|
|
|
|
[name: string]: any
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Represents the resolved data associated with a particular route.
|
|
|
|
*
|
|
|
|
* @see `Route#resolve`.
|
|
|
|
*
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
|
|
|
export type ResolveData = {
|
|
|
|
[name: string]: any
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* A function that is called to resolve a collection of lazy-loaded routes.
|
2019-07-03 19:15:15 -04: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 17:30:09 -04:00
|
|
|
* Often this function will be implemented using an ES dynamic `import()` expression. For example:
|
2019-07-03 19:15:15 -04: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 17:30:09 -04:00
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'lazy',
|
|
|
|
* loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule),
|
|
|
|
* }];
|
|
|
|
* ```
|
2019-07-03 19:15:15 -04: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 17:30:09 -04:00
|
|
|
* This function _must_ match the form above: an arrow function of the form
|
|
|
|
* `() => import('...').then(mod => mod.MODULE)`.
|
2018-05-29 15:25:26 -04:00
|
|
|
*
|
|
|
|
* @see `Route#loadChildren`.
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
2019-04-11 09:23:17 -04:00
|
|
|
export type LoadChildrenCallback = () => Type<any>| NgModuleFactory<any>| Observable<Type<any>>|
|
|
|
|
Promise<NgModuleFactory<any>|Type<any>|any>;
|
2018-05-29 15:25:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* A string of the form `path/to/file#exportName` that acts as a URL for a set of routes to load,
|
|
|
|
* or a function that returns such a set.
|
|
|
|
*
|
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 17:30:09 -04:00
|
|
|
* The string form of `LoadChildren` is deprecated (see `DeprecatedLoadChildren`). The function
|
|
|
|
* form (`LoadChildrenCallback`) should be used instead.
|
|
|
|
*
|
2018-05-29 15:25:26 -04:00
|
|
|
* @see `Route#loadChildren`.
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04: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 17:30:09 -04:00
|
|
|
export type LoadChildren = LoadChildrenCallback | DeprecatedLoadChildren;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A string of the form `path/to/file#exportName` that acts as a URL for a set of routes to load.
|
|
|
|
*
|
|
|
|
* @see `Route#loadChildren`
|
|
|
|
* @publicApi
|
|
|
|
* @deprecated the `string` form of `loadChildren` is deprecated in favor of the proposed ES dynamic
|
|
|
|
* `import()` expression, which offers a more natural and standards-based mechanism to dynamically
|
|
|
|
* load an ES module at runtime.
|
|
|
|
*/
|
|
|
|
export type DeprecatedLoadChildren = string;
|
2018-05-29 15:25:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* How to handle query parameters in a router link.
|
|
|
|
* One of:
|
|
|
|
* - `merge` : Merge new with current parameters.
|
|
|
|
* - `preserve` : Preserve current parameters.
|
|
|
|
*
|
2019-07-03 19:15:15 -04:00
|
|
|
* @see `NavigationExtras#queryParamsHandling`
|
|
|
|
* @see `RouterLink`
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
|
|
|
export type QueryParamsHandling = 'merge' | 'preserve' | '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* A policy for when to run guards and resolvers on a route.
|
|
|
|
*
|
|
|
|
* @see `Route#runGuardsAndResolvers`
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
|
|
|
export type RunGuardsAndResolvers = 'pathParamsChange' | 'pathParamsOrQueryParamsChange' |
|
|
|
|
'paramsChange' | 'paramsOrQueryParamsChange' | 'always' |
|
|
|
|
((from: ActivatedRouteSnapshot, to: ActivatedRouteSnapshot) => boolean);
|
|
|
|
|
|
|
|
/**
|
2019-03-07 17:13:18 -05:00
|
|
|
* 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-05-29 15:25:26 -04:00
|
|
|
*
|
|
|
|
* Supports static, parameterized, redirect, and wildcard routes, as well as
|
|
|
|
* custom route data and resolve methods.
|
|
|
|
*
|
|
|
|
* For detailed usage information, see the [Routing Guide](guide/router).
|
|
|
|
*
|
|
|
|
* @usageNotes
|
|
|
|
*
|
2016-07-06 19:19:52 -04:00
|
|
|
* ### Simple Configuration
|
|
|
|
*
|
2019-03-07 17:13:18 -05: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.
|
2018-05-29 15:25:26 -04:00
|
|
|
*
|
2016-07-06 19:19:52 -04:00
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'team/:id',
|
2016-12-02 17:09:09 -05:00
|
|
|
* component: Team,
|
|
|
|
* children: [{
|
|
|
|
* path: 'user/:name',
|
|
|
|
* component: User
|
|
|
|
* }]
|
2016-07-06 19:19:52 -04:00
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ### Multiple Outlets
|
|
|
|
*
|
2019-03-07 17:13:18 -05: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-05-29 15:25:26 -04:00
|
|
|
*
|
2016-07-06 19:19:52 -04:00
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team
|
2016-12-02 17:09:09 -05:00
|
|
|
* }, {
|
2016-07-06 19:19:52 -04:00
|
|
|
* path: 'chat/:user',
|
|
|
|
* component: Chat
|
2016-09-27 13:10:12 -04:00
|
|
|
* outlet: 'aux'
|
2016-07-06 19:19:52 -04:00
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ### Wild Cards
|
|
|
|
*
|
2019-03-07 17:13:18 -05:00
|
|
|
* The following route uses wild-card notation to specify a component
|
|
|
|
* that is always instantiated regardless of where you navigate to.
|
2018-05-29 15:25:26 -04:00
|
|
|
*
|
2016-07-06 19:19:52 -04:00
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: '**',
|
2019-03-07 17:13:18 -05:00
|
|
|
* component: WildcardComponent
|
2016-07-06 19:19:52 -04:00
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ### Redirects
|
|
|
|
*
|
2019-03-07 17:13:18 -05:00
|
|
|
* The following route uses the `redirectTo` property to ignore a segment of
|
|
|
|
* a given URL when looking for a child path.
|
|
|
|
*
|
|
|
|
* 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-05-29 15:25:26 -04:00
|
|
|
*
|
2016-07-06 19:19:52 -04:00
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
2016-12-02 17:09:09 -05:00
|
|
|
* children: [{
|
|
|
|
* path: 'legacy/user/:name',
|
|
|
|
* redirectTo: 'user/:name'
|
|
|
|
* }, {
|
|
|
|
* path: 'user/:name',
|
|
|
|
* component: User
|
|
|
|
* }]
|
2016-07-06 19:19:52 -04:00
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
2019-03-07 17:13:18 -05: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'.
|
2018-05-29 15:25:26 -04:00
|
|
|
|
2016-07-06 19:19:52 -04:00
|
|
|
* ### Empty Path
|
|
|
|
*
|
2016-09-10 19:52:21 -04:00
|
|
|
* Empty-path route configurations can be used to instantiate components that do not 'consume'
|
2019-03-07 17:13:18 -05:00
|
|
|
* any URL segments.
|
|
|
|
*
|
|
|
|
* In the following configuration, when navigating to
|
|
|
|
* `/team/11`, the router instantiates the 'AllUsers' component.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
2016-12-02 17:09:09 -05:00
|
|
|
* children: [{
|
|
|
|
* path: '',
|
|
|
|
* component: AllUsers
|
|
|
|
* }, {
|
|
|
|
* path: 'user/:name',
|
|
|
|
* component: User
|
|
|
|
* }]
|
2016-07-06 19:19:52 -04:00
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
2018-05-29 15:25:26 -04:00
|
|
|
* Empty-path routes can have children. In the following example, when navigating
|
2019-03-07 17:13:18 -05:00
|
|
|
* to `/team/11/user/jim`, the router instantiates the wrapper component with
|
2018-05-29 15:25:26 -04:00
|
|
|
* the user component in it.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
2018-05-29 15:25:26 -04:00
|
|
|
* Note that an empty path route inherits its parent's parameters and data.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'team/:id',
|
|
|
|
* component: Team,
|
2016-12-02 17:09:09 -05:00
|
|
|
* children: [{
|
|
|
|
* path: '',
|
|
|
|
* component: WrapperCmp,
|
|
|
|
* children: [{
|
|
|
|
* path: 'user/:name',
|
|
|
|
* component: User
|
|
|
|
* }]
|
|
|
|
* }]
|
2016-07-06 19:19:52 -04:00
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ### Matching Strategy
|
|
|
|
*
|
2018-05-29 15:25:26 -04: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'.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: '',
|
|
|
|
* pathMatch: 'prefix', //default
|
|
|
|
* redirectTo: 'main'
|
2016-12-02 17:09:09 -05:00
|
|
|
* }, {
|
2016-07-06 19:19:52 -04:00
|
|
|
* path: 'main',
|
|
|
|
* component: Main
|
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
2018-05-29 15:25:26 -04:00
|
|
|
* 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 19:19:52 -04:00
|
|
|
*
|
2019-10-01 08:57:05 -04:00
|
|
|
* In the following example, supplying the 'full' `pathMatch` strategy ensures
|
2018-05-29 15:25:26 -04:00
|
|
|
* that the router applies the redirect if and only if navigating to '/'.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: '',
|
|
|
|
* pathMatch: 'full',
|
|
|
|
* redirectTo: 'main'
|
2016-12-02 17:09:09 -05:00
|
|
|
* }, {
|
2016-07-06 19:19:52 -04:00
|
|
|
* path: 'main',
|
|
|
|
* component: Main
|
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ### Componentless Routes
|
|
|
|
*
|
2018-05-29 15:25:26 -04: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.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
2019-07-24 03:07:35 -04:00
|
|
|
* In the following example, 'MainChild' and 'AuxChild' are siblings.
|
2018-05-29 15:25:26 -04:00
|
|
|
* 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.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'parent/:id',
|
|
|
|
* children: [
|
|
|
|
* { path: 'a', component: MainChild },
|
|
|
|
* { path: 'b', component: AuxChild, outlet: 'aux' }
|
|
|
|
* ]
|
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
2018-05-29 15:25:26 -04:00
|
|
|
* The router merges the parameters, data, and resolve of the componentless
|
|
|
|
* parent into the parameters, data, and resolve of the children.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
2018-05-29 15:25:26 -04: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.
|
2016-07-06 19:19:52 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
|
|
|
* path: 'parent/:id',
|
|
|
|
* children: [
|
|
|
|
* { path: '', component: MainChild },
|
|
|
|
* { path: '', component: AuxChild, outlet: 'aux' }
|
|
|
|
* ]
|
|
|
|
* }]
|
|
|
|
* ```
|
|
|
|
*
|
2016-09-10 19:52:21 -04:00
|
|
|
* ### Lazy Loading
|
|
|
|
*
|
2018-05-29 15:25:26 -04: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 instead of the `children` property.
|
|
|
|
*
|
2019-09-26 08:41:59 -04:00
|
|
|
* Given the following example route, the router will lazy load
|
|
|
|
* the associated module on demand using the browser native import system.
|
2016-09-10 19:52:21 -04:00
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* [{
|
2019-09-26 08:41:59 -04:00
|
|
|
* path: 'lazy',
|
|
|
|
* loadChildren: () => import('./lazy-route/lazy.module').then(mod => mod.LazyModule),
|
|
|
|
* }];
|
2016-09-10 19:52:21 -04:00
|
|
|
* ```
|
|
|
|
*
|
2019-03-05 14:32:29 -05:00
|
|
|
* @publicApi
|
2016-06-27 15:27:23 -04:00
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
export interface Route {
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
2019-10-02 13:54:57 -04:00
|
|
|
* The path to match against. Cannot be used together with a custom `matcher` function.
|
|
|
|
* A URL string that uses router matching notation.
|
2019-04-02 14:01:48 -04:00
|
|
|
* Can be a wild card (`**`) that matches any URL (see Usage Notes below).
|
2018-05-29 15:25:26 -04:00
|
|
|
* Default is "/" (the root path).
|
2019-10-02 13:54:57 -04:00
|
|
|
*
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
path?: string;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* The path-matching strategy, one of 'prefix' or 'full'.
|
|
|
|
* Default is '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'.
|
2019-04-02 14:01:48 -04:00
|
|
|
*
|
2018-05-29 15:25:26 -04:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2016-07-28 15:15:07 -04:00
|
|
|
pathMatch?: string;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
2019-10-02 13:54:57 -04:00
|
|
|
* A custom URL-matching function. Cannot be used together with `path`.
|
2018-05-29 15:25:26 -04:00
|
|
|
*/
|
2016-11-09 18:25:47 -05:00
|
|
|
matcher?: UrlMatcher;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* The component to instantiate when the path matches.
|
|
|
|
* Can be empty if child routes specify components.
|
|
|
|
*/
|
2016-08-16 16:40:28 -04:00
|
|
|
component?: Type<any>;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* A URL to which to redirect when a the path matches.
|
|
|
|
* Absolute if the URL begins with a slash (/), otherwise relative to the path URL.
|
|
|
|
* When not present, router does not redirect.
|
|
|
|
*/
|
2016-06-28 17:49:29 -04:00
|
|
|
redirectTo?: string;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* Name of a `RouterOutlet` object where the component can be placed
|
|
|
|
* when the path matches.
|
|
|
|
*/
|
2016-05-23 19:14:23 -04:00
|
|
|
outlet?: string;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-06-07 12:50:35 -04:00
|
|
|
canActivate?: any[];
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-07-13 18:15:20 -04:00
|
|
|
canActivateChild?: any[];
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2016-06-07 12:50:35 -04:00
|
|
|
canDeactivate?: any[];
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-07-26 17:39:02 -04:00
|
|
|
canLoad?: any[];
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* Additional developer-defined data provided to the component via
|
|
|
|
* `ActivatedRoute`. By default, no additional data is passed.
|
|
|
|
*/
|
2016-06-27 17:00:07 -04:00
|
|
|
data?: Data;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* A map of DI tokens used to look up data resolvers. See `Resolve`.
|
|
|
|
*/
|
2016-06-27 17:00:07 -04:00
|
|
|
resolve?: ResolveData;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* An array of child `Route` objects that specifies a nested route
|
|
|
|
* configuration.
|
|
|
|
*/
|
2016-12-06 13:41:01 -05:00
|
|
|
children?: Routes;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* A `LoadChildren` object specifying lazy-loaded child routes.
|
|
|
|
*/
|
2016-08-16 00:11:09 -04:00
|
|
|
loadChildren?: LoadChildren;
|
2018-05-29 15:25:26 -04:00
|
|
|
/**
|
|
|
|
* Defines when guards and resolvers will be run. One of
|
|
|
|
* - `paramsOrQueryParamsChange` : Run when query parameters change.
|
|
|
|
* - `always` : Run on every execution.
|
|
|
|
* By default, guards and resolvers run only when the matrix
|
|
|
|
* parameters of the route change.
|
|
|
|
*/
|
2017-02-24 01:12:30 -05:00
|
|
|
runGuardsAndResolvers?: RunGuardsAndResolvers;
|
2017-04-11 11:34:58 -04:00
|
|
|
/**
|
|
|
|
* Filled for routes with `loadChildren` once the module has been loaded
|
|
|
|
* @internal
|
|
|
|
*/
|
2017-04-04 19:00:40 -04:00
|
|
|
_loadedConfig?: LoadedRouterConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LoadedRouterConfig {
|
|
|
|
constructor(public routes: Route[], public module: NgModuleRef<any>) {}
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 13:41:01 -05:00
|
|
|
export function validateConfig(config: Routes, parentPath: string = ''): void {
|
2016-11-10 17:55:10 -05:00
|
|
|
// forEach doesn't iterate undefined values
|
|
|
|
for (let i = 0; i < config.length; i++) {
|
2016-12-06 13:41:01 -05:00
|
|
|
const route: Route = config[i];
|
|
|
|
const fullPath: string = getFullPath(parentPath, route);
|
|
|
|
validateNode(route, fullPath);
|
2016-11-10 17:55:10 -05:00
|
|
|
}
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 13:41:01 -05:00
|
|
|
function validateNode(route: Route, fullPath: string): void {
|
2016-11-10 17:55:10 -05:00
|
|
|
if (!route) {
|
|
|
|
throw new Error(`
|
2016-12-06 13:41:01 -05:00
|
|
|
Invalid configuration of route '${fullPath}': Encountered undefined route.
|
2016-11-10 17:55:10 -05:00
|
|
|
The reason might be an extra comma.
|
2017-02-24 01:12:30 -05:00
|
|
|
|
|
|
|
Example:
|
2016-11-10 17:55:10 -05:00
|
|
|
const routes: Routes = [
|
|
|
|
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
|
|
|
|
{ path: 'dashboard', component: DashboardComponent },, << two commas
|
|
|
|
{ path: 'detail/:id', component: HeroDetailComponent }
|
|
|
|
];
|
|
|
|
`);
|
|
|
|
}
|
2016-07-14 11:28:31 -04:00
|
|
|
if (Array.isArray(route)) {
|
2016-12-06 13:41:01 -05:00
|
|
|
throw new Error(`Invalid configuration of route '${fullPath}': Array cannot be specified`);
|
2016-07-14 11:28:31 -04:00
|
|
|
}
|
2018-04-06 18:56:36 -04:00
|
|
|
if (!route.component && !route.children && !route.loadChildren &&
|
|
|
|
(route.outlet && route.outlet !== PRIMARY_OUTLET)) {
|
2016-10-20 18:00:15 -04:00
|
|
|
throw new Error(
|
2018-04-06 18:56:36 -04:00
|
|
|
`Invalid configuration of route '${fullPath}': a componentless route without children or loadChildren cannot have a named outlet set`);
|
2016-10-20 18:00:15 -04:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.redirectTo && route.children) {
|
2016-06-16 17:36:51 -04:00
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}': redirectTo and children cannot be used together`);
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.redirectTo && route.loadChildren) {
|
2016-07-06 14:02:16 -04:00
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}': redirectTo and loadChildren cannot be used together`);
|
2016-07-06 14:02:16 -04:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.children && route.loadChildren) {
|
2016-07-06 14:02:16 -04:00
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}': children and loadChildren cannot be used together`);
|
2016-07-06 14:02:16 -04:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.redirectTo && route.component) {
|
2016-06-16 17:36:51 -04:00
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}': redirectTo and component cannot be used together`);
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.path && route.matcher) {
|
2016-11-09 18:25:47 -05:00
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}': path and matcher cannot be used together`);
|
2016-11-09 18:25:47 -05:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.redirectTo === void 0 && !route.component && !route.children && !route.loadChildren) {
|
2016-06-19 17:44:20 -04:00
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}'. One of the following must be provided: component, redirectTo, children or loadChildren`);
|
2016-06-19 17:44:20 -04:00
|
|
|
}
|
2016-12-02 17:17:27 -05:00
|
|
|
if (route.path === void 0 && route.matcher === void 0) {
|
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}': routes must have either a path or a matcher specified`);
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
2016-12-02 17:17:27 -05:00
|
|
|
if (typeof route.path === 'string' && route.path.charAt(0) === '/') {
|
2016-12-06 13:41:01 -05:00
|
|
|
throw new Error(`Invalid configuration of route '${fullPath}': path cannot start with a slash`);
|
2016-06-16 17:36:51 -04:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.path === '' && route.redirectTo !== void 0 && route.pathMatch === void 0) {
|
2016-06-27 23:10:36 -04:00
|
|
|
const exp =
|
|
|
|
`The default value of 'pathMatch' is 'prefix', but often the intent is to use 'full'.`;
|
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '{path: "${fullPath}", redirectTo: "${route.redirectTo}"}': please provide 'pathMatch'. ${exp}`);
|
2016-06-27 23:10:36 -04:00
|
|
|
}
|
2016-12-02 17:09:09 -05:00
|
|
|
if (route.pathMatch !== void 0 && route.pathMatch !== 'full' && route.pathMatch !== 'prefix') {
|
2016-07-29 12:59:50 -04:00
|
|
|
throw new Error(
|
2016-12-06 13:41:01 -05:00
|
|
|
`Invalid configuration of route '${fullPath}': pathMatch can only be set to 'prefix' or 'full'`);
|
|
|
|
}
|
|
|
|
if (route.children) {
|
|
|
|
validateConfig(route.children, fullPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFullPath(parentPath: string, currentRoute: Route): string {
|
|
|
|
if (!currentRoute) {
|
|
|
|
return parentPath;
|
|
|
|
}
|
|
|
|
if (!parentPath && !currentRoute.path) {
|
|
|
|
return '';
|
|
|
|
} else if (parentPath && !currentRoute.path) {
|
|
|
|
return `${parentPath}/`;
|
|
|
|
} else if (!parentPath && currentRoute.path) {
|
|
|
|
return currentRoute.path;
|
|
|
|
} else {
|
|
|
|
return `${parentPath}/${currentRoute.path}`;
|
2016-07-29 12:59:50 -04:00
|
|
|
}
|
2016-06-27 15:27:23 -04:00
|
|
|
}
|
2018-02-21 13:21:47 -05:00
|
|
|
|
2018-04-06 18:56:36 -04:00
|
|
|
/**
|
|
|
|
* Makes a copy of the config and adds any default required properties.
|
|
|
|
*/
|
|
|
|
export function standardizeConfig(r: Route): Route {
|
|
|
|
const children = r.children && r.children.map(standardizeConfig);
|
|
|
|
const c = children ? {...r, children} : {...r};
|
|
|
|
if (!c.component && (children || c.loadChildren) && (c.outlet && c.outlet !== PRIMARY_OUTLET)) {
|
|
|
|
c.component = EmptyOutletComponent;
|
|
|
|
}
|
|
|
|
return c;
|
2018-02-27 17:06:06 -05:00
|
|
|
}
|