You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Route redirects allow you to automatically navigate users from one route to another. Think of it like mail forwarding, where mail intended for one address is sent to a different address. This is useful for handling legacy URLs, implementing default routes, or managing access control.
4
+
5
+
## How to configure redirects
6
+
7
+
You can define redirects in your route configuration with the `redirectTo` property. This property accepts a string.
In this example, all routes that are prefixed with `news` are redirected to their `/blog` equivalents. Here are some examples where users are redirected when visiting the old `news` prefix:
59
+
60
+
- `/news` redirects to `/blog`
61
+
- `/news/article` redirects to `/blog/article`
62
+
- `/news/article/:id` redirects to `/blog/article/:id`
63
+
64
+
### `pathMatch: 'full'`
65
+
66
+
On the other hand, `pathMatch: 'full'` is useful when you want Angular Router to only redirect a specific path.
In this example, any time the user visits the root URL (i.e., `''`), the router redirects that user to the `'/dashboard'` page.
75
+
76
+
Any subsequent pages (e.g., `/login`, `/about`, `/product/id`, etc.), are ignored and do not trigger a redirect.
77
+
78
+
TIP: Be careful when configuring a redirect on the root page (i.e., `"/"` or `""`). If you do not set `pathMatch: 'full'`, the router will redirect all URLs.
79
+
80
+
To further illustrate this, if the `news` example from the previous section used `pathMatch: 'full'` instead:
1. Only the `/news` path will be redirected to `/blog`.
91
+
2. Any subsequent segments such as `/news/articles` or `/news/articles/1` would not redirect with the new `/blog` prefix.
92
+
93
+
## Conditional redirects
94
+
95
+
The `redirectTo` property can also accept a function in order to add logic to how users are redirected.
96
+
97
+
The [function](api/router/RedirectFunction) only has access part of the [`ActivatedRouteSnapshot`](api/router/ActivatedRouteSnapshot) data since some data is not accurately known at the route matching phase. Examples include: resolved titles, lazy loaded components, etc.
98
+
99
+
It typically returns a string or [`URLTree`](api/router/UrlTree), but it can also return an observable or promise.
100
+
101
+
Here is an example where the user is redirected to different menu based on the time of the day:
Copy file name to clipboardExpand all lines: adev-ja/src/content/guide/routing/redirecting-routes.md
+38-38Lines changed: 38 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
-
# Redirecting Routes
1
+
# ルートのリダイレクト
2
2
3
-
Route redirects allow you to automatically navigate users from one route to another. Think of it like mail forwarding, where mail intended for one address is sent to a different address. This is useful for handling legacy URLs, implementing default routes, or managing access control.
In this example, all routes that are prefixed with `news` are redirected to their `/blog` equivalents. Here are some examples where users are redirected when visiting the old `news` prefix:
TIP: Be careful when configuring a redirect on the root page (i.e., `"/"` or `""`). If you do not set `pathMatch: 'full'`, the router will redirect all URLs.
The [function](api/router/RedirectFunction) only has access part of the [`ActivatedRouteSnapshot`](api/router/ActivatedRouteSnapshot) data since some data is not accurately known at the route matching phase. Examples include: resolved titles, lazy loaded components, etc.
0 commit comments