skills/dev-skills/angular-developer/references/define-routes.md
Routes are objects that define which component should render for a specific URL path.
Define routes in a Routes array and provide them using provideRouter in your appConfig.
// app.routes.ts
export const routes: Routes = [
{path: '', component: HomePage},
{path: 'admin', component: AdminPage},
];
// app.config.ts
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
};
'admin').'user/:id').**. Useful for "Not Found" pages. Always place at the end of the array.Angular uses a first-match wins strategy. Specific routes must come before less specific ones.
Use redirectTo to point one path to another.
{ path: 'articles', redirectTo: '/blog' },
{ path: 'blog', component: Blog },
Pass a function to redirectTo to apply logic when redirecting.
{
path: 'restaurant/:location/menu',
redirectTo: ({ params }) => {
const base = `/restaurant/${params['location']}/menu`;
const hour = new Date().getHours();
if (hour < 11) return `${base}/breakfast`;
if (hour < 17) return `${base}/lunch`;
return `${base}/dinner`;
},
},
Associate titles with routes for accessibility. Titles can be static or dynamic (via ResolveFn or a custom TitleStrategy).
{ path: 'home', component: Home, title: 'Home Page' }
data property.providers array.Define sub-views using the children property. Parent components must include a <router-outlet />.
{
path: 'product/:id',
component: Product,
children: [
{ path: 'info', component: ProductInfo },
{ path: 'reviews', component: ProductReviews },
],
}