d48cda4081
CI / Test (ubuntu-latest, Node 18.x, bun) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, npm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, pnpm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, yarn) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 20.x, bun) (push) Failing after 17m13s
CI / Test (ubuntu-latest, Node 20.x, npm) (push) Failing after 18m42s
CI / Test (ubuntu-latest, Node 20.x, pnpm) (push) Failing after 15m0s
CI / Test (ubuntu-latest, Node 20.x, yarn) (push) Failing after 49m44s
CI / Test (ubuntu-latest, Node 22.x, bun) (push) Failing after 51m55s
CI / Test (ubuntu-latest, Node 22.x, pnpm) (push) Failing after 21m57s
CI / Test (ubuntu-latest, Node 22.x, npm) (push) Failing after 37m39s
CI / Test (ubuntu-latest, Node 22.x, yarn) (push) Failing after 34m7s
CI / Validate Components (push) Failing after 37m15s
CI / Python Tests (push) Failing after 10m1s
CI / Security Scan (push) Failing after 10m1s
CI / Lint (push) Failing after 17m12s
CI / Coverage (push) Failing after 20m19s
CI / Test (macos-latest, Node 18.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, yarn) (push) Has been cancelled
46 lines
1.5 KiB
Markdown
46 lines
1.5 KiB
Markdown
# Router Lifecycle and Events
|
|
|
|
Angular Router emits events through the `Router.events` observable, allowing you to track the navigation lifecycle from start to finish.
|
|
|
|
## Common Router Events (Chronological)
|
|
|
|
1. **`NavigationStart`**: Navigation begins.
|
|
2. **`RoutesRecognized`**: Router matches the URL to a route.
|
|
3. **`GuardsCheckStart` / `End`**: Evaluation of `canActivate`, `canMatch`, etc.
|
|
4. **`ResolveStart` / `End`**: Data resolution phase (fetching data via resolvers).
|
|
5. **`NavigationEnd`**: Navigation completed successfully.
|
|
6. **`NavigationCancel`**: Navigation canceled (e.g., guard returned `false`).
|
|
7. **`NavigationError`**: Navigation failed (e.g., error in resolver).
|
|
|
|
## Subscribing to Events
|
|
|
|
Inject the `Router` and filter the `events` observable.
|
|
|
|
```ts
|
|
import {Router, NavigationStart, NavigationEnd} from '@angular/router';
|
|
|
|
export class MyService {
|
|
private router = inject(Router);
|
|
|
|
constructor() {
|
|
this.router.events.pipe(filter((e) => e instanceof NavigationEnd)).subscribe((event) => {
|
|
console.log('Navigated to:', event.url);
|
|
});
|
|
}
|
|
}
|
|
```
|
|
|
|
## Debugging
|
|
|
|
Enable detailed console logging of all routing events during application bootstrap.
|
|
|
|
```ts
|
|
provideRouter(routes, withDebugTracing());
|
|
```
|
|
|
|
## Common Use Cases
|
|
|
|
- **Loading Indicators**: Show a spinner when `NavigationStart` fires and hide it on `NavigationEnd`/`Cancel`/`Error`.
|
|
- **Analytics**: Track page views by listening for `NavigationEnd`.
|
|
- **Scroll Management**: Respond to `Scroll` events for custom scroll behavior.
|