When applied to an element in a template, makes that element a link that initiates navigation to a route. Navigation opens one or more routed components in one or more <router-outlet> locations on the page.
API
class RouterLink implements OnChanges ,OnDestroy {
constructor(router: Router, route: ActivatedRoute, tabIndexAttribute: string | null | undefined, renderer: Renderer2, el: ElementRef<any>, locationStrategy?: LocationStrategy | undefined): RouterLink;
get href(): string | null;
@Input() get target(): string | undefined;
@Input() get queryParams(): Params | null | undefined;
@Input() get fragment(): string | undefined;
@Input() get queryParamsHandling(): QueryParamsHandling | null | undefined;
@Input() get state(): { [k: string]: any; } | undefined;
@Input() get info(): unknown;
@Input() get relativeTo(): ActivatedRoute | null | undefined;
@Input() get preserveFragment(): boolean;
@Input() get skipLocationChange(): boolean;
@Input() get replaceUrl(): boolean;
@Input() set routerLink(value: string | readonly any[] | UrlTree | null | undefined);
readonly urlTree: UrlTree | null;
}
constructor
RouterLinkstring | null | undefined
RouterLink
href
string | nullRepresents an href attribute value applied to a host element, when a host element is an <a>/<area> tag or a compatible custom element. For other tags, the value is null.
href
string | nulltarget
string | undefinedRepresents the target attribute on a host element. This is only used when the host element is an <a>/<area> tag or a compatible custom element.
target
string | undefinedqueryParams
Params | null | undefinedPassed to Router#createUrlTree as part of the UrlCreationOptions.
queryParams
Params | null | undefinedfragment
string | undefinedPassed to Router#createUrlTree as part of the UrlCreationOptions.
fragment
string | undefinedqueryParamsHandling
QueryParamsHandling | null | undefinedPassed to Router#createUrlTree as part of the UrlCreationOptions.
queryParamsHandling
QueryParamsHandling | null | undefinedstate
{ [k: string]: any; } | undefinedPassed to Router#navigateByUrl as part of the NavigationBehaviorOptions.
state
{ [k: string]: any; } | undefinedinfo
unknownPassed to Router#navigateByUrl as part of the NavigationBehaviorOptions.
info
unknownrelativeTo
ActivatedRoute | null | undefinedPassed to Router#createUrlTree as part of the UrlCreationOptions. Specify a value here when you do not want to use the default value for routerLink, which is the current activated route. Note that a value of undefined here will use the routerLink default.
relativeTo
ActivatedRoute | null | undefinedpreserveFragment
booleanPassed to Router#createUrlTree as part of the UrlCreationOptions.
preserveFragment
booleanskipLocationChange
booleanPassed to Router#navigateByUrl as part of the NavigationBehaviorOptions.
skipLocationChange
booleanreplaceUrl
booleanPassed to Router#navigateByUrl as part of the NavigationBehaviorOptions.
replaceUrl
booleanrouterLink
string | readonly any[] | UrlTree | null | undefinedCommands to pass to Router#createUrlTree or a UrlTree.
-
array: commands to pass to
Router#createUrlTree. -
string: shorthand for array of commands with just the string, i.e.
['/route'] -
UrlTree: a
UrlTreefor this link rather than creating one from the commands and other inputs that correspond to properties ofUrlCreationOptions. -
null|undefined: effectively disables the
routerLink
urlTree
UrlTree | nullDescription
When applied to an element in a template, makes that element a link that initiates navigation to a route. Navigation opens one or more routed components in one or more <router-outlet> locations on the page.
Given a route configuration [{ path: 'user/:name', component: UserCmp }], the following creates a static link to the route: <a routerLink="/user/bob">link to user component</a>
You can use dynamic values to generate the link. For a dynamic link, pass an array of path segments, followed by the params for each segment. For example, ['/team', teamId, 'user', userName, {details: true}] generates a link to /team/11/user/bob;details=true.
Multiple static segments can be merged into one term and combined with dynamic segments. For example, ['/team/11/user', userName, {details: true}]
The input that you provide to the link is treated as a delta to the current URL. For instance, suppose the current URL is /user/(box//aux:team). The link <a [routerLink]="['/user/jim']">Jim</a> creates the URL /user/(jim//aux:team). See Router#createUrlTree for more information.
Exported by
Usage Notes
You can use absolute or relative paths in a link, set query parameters, control how parameters are handled, and keep a history of navigation states.
Relative link paths
The first segment name can be prepended with /, ./, or ../.
- If the first segment begins with
/, the router looks up the route from the root of the app. - If the first segment begins with
./, or doesn't begin with a slash, the router looks in the children of the current activated route. - If the first segment begins with
../, the router goes up one level in the route tree.
Setting and handling query params and fragments
The following link adds a query parameter and a fragment to the generated URL:
<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}"
fragment="education"> link to user component
</a>
By default, the directive constructs the new URL using the given query parameters. The example generates the link: /user/bob?debug=true#education.
You can instruct the directive to handle query parameters differently by specifying the queryParamsHandling option in the link. Allowed values are:
-
'merge': Merge the givenqueryParamsinto the current query params. -
'preserve': Preserve the current query params.
For example:
<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}"
queryParamsHandling="merge"> link to user component
</a>
queryParams, fragment, queryParamsHandling, preserveFragment, and relativeTo cannot be used when the routerLink input is a UrlTree.
See UrlCreationOptions#queryParamsHandling.
Preserving navigation history
You can provide a state value to be persisted to the browser's History.state property. For example:
<a [routerLink]="['/user/bob']" [state]="{tracingId: 123}">
link to user component
</a>
Use Router#currentNavigation to retrieve a saved Signal navigation-state value. For example, to capture the tracingId during the NavigationStart event:
// Get NavigationStart events
router.events.pipe(filter(e => e instanceof NavigationStart)).subscribe(e => {
const navigation = router.currentNavigation();
tracingService.trace({id: navigation.extras.state.tracingId});
}); RouterLink compatible custom elements
In order to make a custom element work with routerLink, the corresponding custom element must implement the href attribute and must list href in the array of the static property/getter observedAttributes.