ion-route-redirect
A route redirect can only be used with an ion-router
as a direct child of it.
Note: this component should only be used with vanilla and Stencil JavaScript projects. For Angular projects, use
ion-router-outlet
and the Angular router.
The route redirect has two configurable properties:
from
to
It redirects "from" a URL "to" another URL. When the defined ion-route-redirect
rule matches, the router will redirect from the path specified in the from
property to the path in the to
property. In order for a redirect to occur the from
path needs to be an exact match to the navigated URL.
#
Multiple Route RedirectsAn arbitrary number of redirect routes can be defined inside an ion-router
, but only one can match.
A route redirect will never call another redirect after its own redirect, since this could lead to infinite loops.
Take the following two redirects:
If the user navigates to /admin
the router will redirect to /login
and stop there. It will never evaluate more than one redirect.
#
Usage#
Route Redirects as GuardsRedirection routes can work as guards to prevent users from navigating to certain areas of an application based on a given condition, such as if the user is authenticated or not.
A route redirect can be added and removed dynamically to redirect (or guard) some routes from being accessed. In the following example, all urls *
will be redirected to the /login
url if isLoggedIn
is false
.
Alternatively, the value of to
can be modified based on a condition. In the following example, the route redirect will check if the user is logged in and redirect to the /login
url if not.
#
Properties#
fromDescription | A redirect route, redirects "from" a URL "to" another URL. This property is that "from" URL. It needs to be an exact match of the navigated URL in order to apply. The path specified in this value is always an absolute path, even if the initial / slashis not specified. |
Attribute | from |
Type | string |
Default | undefined |
#
toDescription | A redirect route, redirects "from" a URL "to" another URL. This property is that "to" URL. When the defined ion-route-redirect rule matches, the router will redirect to the pathspecified in this property. The value of this property is always an absolute path inside the scope of routes defined in ion-router it can't be used with another router or to perform a redirection to a different domain.Note that this is a virtual redirect, it will not cause a real browser refresh, again, it's a redirect inside the context of ion-router. When this property is not specified or his value is undefined the whole redirect route is noop,even if the "from" value matches. |
Attribute | to |
Type | null \| string \| undefined |
Default | undefined |
#
EventsName | Description |
---|---|
ionRouteRedirectChanged | Internal event that fires when any value of this rule is added/removed from the DOM, |
or any of his public properties changes.
ion-router
captures this event in order to update his internal registry of router rules. |