{ "id": "api/router/Route", "title": "Route", "contents": "\n\n
\n
\n
\n \n API > @angular/router\n
\n \n
\n \n
\n

Routelink

\n \n \n \n \n \n
\n \n \n\n
\n \n
\n

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

\n\n

See more...

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

Descriptionlink

\n

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

\n

For detailed usage information, see the Routing Guide.

\n\n

Further information available in the Usage Notes...

\n
\n\n \n
\n

Propertieslink

\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
PropertyDescription
\n \n path?: string\n \n \n

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

\n\n \n
\n \n pathMatch?: string\n \n \n

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

\n\n

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

\n

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

\n\n
\n \n matcher?: UrlMatcher\n \n \n

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

\n\n \n
\n \n component?: Type<any>\n \n \n

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

\n\n \n
\n \n redirectTo?: string\n \n \n

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

\n\n \n
\n \n outlet?: string\n \n \n

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

\n\n \n
\n \n canActivate?: any[]\n \n \n

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

\n\n \n
\n \n canActivateChild?: any[]\n \n \n

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

\n\n \n
\n \n canDeactivate?: any[]\n \n \n

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

\n\n \n
\n \n canLoad?: any[]\n \n \n

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

\n\n \n
\n \n data?: Data\n \n \n

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

\n\n \n
\n \n resolve?: ResolveData\n \n \n

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

\n\n \n
\n \n children?: Routes\n \n \n

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

\n\n \n
\n \n loadChildren?: LoadChildren\n \n \n

An object specifying lazy-loaded child routes.

\n\n \n
\n \n runGuardsAndResolvers?: RunGuardsAndResolvers\n \n \n

Defines when guards and resolvers will be run. One of

\n
    \n
  • paramsOrQueryParamsChange : Run when query parameters change.
  • \n
  • always : Run on every execution.\nBy default, guards and resolvers run only when the matrix\nparameters of the route change.
  • \n
\n\n \n
\n
\n \n\n\n \n
\n

Usage noteslink

\n

Simple Configurationlink

\n

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

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

Multiple Outletslink

\n

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

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

Wild Cardslink

\n

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

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

Redirectslink

\n

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

\n

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

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

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

\n

Empty Pathlink

\n

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

\n

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

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

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

\n

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

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

Matching Strategylink

\n

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

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

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

\n

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

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

Componentless Routeslink

\n

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

\n

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

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

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

\n

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

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

Lazy Loadinglink

\n

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

\n

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

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