Routing
Analog supports filesystem-based routing on top of the Angular Router.
Defining Routes
Routes are defined using folders and files in the src/app/pages
folder. Only files ending with .page.ts
are collected and used to build the set of routes.
Route components must be defined as the default export and all route components are lazy-loaded.
There are 5 primary types of routes:
These routes can be combined in different ways to build URLs for navigation.
In addition to the 5 primary types of routes, Analog also supports Redirect Routes and Content Routes.
Index Routes
Index routes are defined by using the filename as the route path enclosed in parenthesis.
The example route below in src/app/pages/(home).page.ts
defines an /
route.
import { Component } from '@angular/core';
@Component({
standalone: true,
template: ` <h2>Welcome</h2> `,
})
export default class HomePageComponent {}
Index routes can also be defined by using index.page.ts
as the route filename.
Static Routes
Static routes are defined by using the filename as the route path.
The example route below in src/app/pages/about.page.ts
defines an /about
route.
import { Component } from '@angular/core';
@Component({
standalone: true,
template: `
<h2>Hello Analog</h2>
Analog is a meta-framework on top of Angular.
`,
})
export default class AboutPageComponent {}
It's also possible to define nested static routes in two different ways:
- By nesting the route files in folders -
src/app/pages/about/team.page.ts
defines an/about/team
route. - By using the dot notation in the filename -
src/app/pages/about.team.page.ts
also defines an/about/team
route.
Route Groups
Routes can be grouped together in the same folder without adding a route path segment by wrapping a folder name in parenthesis.
src/
└── app/
└── pages/
└── (auth)/
├── login.page.ts
└── signup.page.ts
The above example defines /login
and /signup
routes.
Dynamic Routes
Dynamic routes are defined by using the filename as the route path enclosed in square brackets. The parameter for the route is extracted from the route path.
The example route below in src/app/pages/products/[productId].page.ts
defines a /products/:productId
route.
import { Component, inject } from '@angular/core';
import { AsyncPipe } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { map } from 'rxjs';
@Component({
standalone: true,
imports: [AsyncPipe],
template: `
<h2>Product Details</h2>
ID: {{ productId$ | async }}
`,
})
export default class ProductDetailsPageComponent {
private readonly route = inject(ActivatedRoute);
readonly productId$ = this.route.paramMap.pipe(
map((params) => params.get('productId'))
);
}
Dynamic routes can also be defined using the dot notation in the filename - src/app/pages/products.[productId].page.ts
defines a /products/:productId
route.
Using Route Component Input Bindings
If you are using the withComponentInputBinding()
feature with the Angular Router, you can use the Input decorator, along with the same parameter name to get the route parameter.
First, add the withComponentInputBinding()
to the arguments for the provideFileRouter()
function.
// src/app/app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideFileRouter } from '@analogjs/router';
import { withComponentInputBinding } from '@angular/router';
export const appConfig: ApplicationConfig = {
providers: [
provideFileRouter(withComponentInputBinding()),
// other providers
],
};
Next, use the route parameter as an input.
// src/app/pages/products/[productId].page.ts
import { Component, Input } from '@angular/core';
@Component({
standalone: true,
template: `
<h2>Product Details</h2>
ID: {{ productId }}
`,
})
export default class ProductDetailsPageComponent {
@Input() productId: string;
}