What is @angular/router?
The @angular/router package provides navigation and URL manipulation capabilities for Angular applications. It allows developers to define routes, navigate between them, handle route parameters, and guard routes among other functionalities.
What are @angular/router's main functionalities?
Route Configuration
Defines routes in an Angular application, associating paths with components.
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Router Outlet
A placeholder that Angular dynamically fills based on the current router state.
<router-outlet></router-outlet>
Navigation
Programmatically navigate to different routes within an Angular application.
constructor(private router: Router) { }
navigateToHome() {
this.router.navigate(['/home']);
}
Route Parameters
Access parameters for a given route, often used for fetching data based on the URL.
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.paramMap.subscribe(params => {
this.userId = params.get('id');
});
}
Route Guards
Protect routes with guards that can allow or prevent navigation to a route based on logic such as authentication.
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.isAuthenticated();
}
}
Other packages similar to @angular/router
react-router
React Router is a routing library for React, similar to @angular/router but for React applications. It provides components for navigation, route matching, and history handling. While @angular/router is designed specifically for Angular, React Router is tailored to React's component structure.
vue-router
Vue Router is the official router for Vue.js. It integrates deeply with Vue.js core to make building Single Page Applications with Vue.js a breeze. It offers features similar to @angular/router but is designed to work seamlessly with Vue.js components and reactivity system.
reach-router
Reach Router is a small, simple router for React that is accessible and easy to use. It is similar to @angular/router in providing navigation and route matching, but it focuses on simplicity and accessibility. It has now been succeeded by React Router, which has incorporated its accessibility features.
17.1.0 (2024-01-17)
compiler
| Commit | Type | Description |
| -- | -- | -- |
| 79ff91a813 | fix | allow TS jsDocParsingMode host option to be programmatically set (#53126) |
| 5613051a8b | fix | allow TS jsDocParsingMode host option to be programmatically set again (#53292) |
| df8a825910 | fix | project empty block root node (#53620) |
| 478d622265 | fix | project empty block root node in template pipeline (#53620) |
compiler-cli
| Commit | Type | Description |
| -- | -- | -- |
| abdc7e4578 | feat | support type-checking for generic signal inputs (#53521) |
| e620b3a724 | fix | add compiler option to disable control flow content projection diagnostic (#53311) |
| 4c1d69e288 | fix | add diagnostic for control flow that prevents content projection (#53190) |
| 76ceebad04 | fix | do not throw fatal error if extended type check fails (#53896) |
| 1a6eaa0fea | fix | input transform in local compilation mode (#53645) |
| 56a76d73e0 | fix | modify getConstructorDependencies
helper to work with reflection host after the previous change (#52215) |
core
| Commit | Type | Description |
| -- | -- | -- |
| 863be4b698 | feat | expose new input
API for signal-based inputs (#53872) |
| 94096c6ede | feat | support TypeScript 5.3 (#52572) |
| 69b384c0d1 | fix | SignalNode
reactive node incorrectly exposing unset field (#53571) |
| 6f79507ea7 | fix | Change defer block fixture default behavior to playthrough (#53956) |
| 32f908ab70 | fix | do not accidentally inherit input transforms when overridden (#53571) |
| bdd61c768a | fix | replace assertion with more intentional error (#52234) |
| 0daca457bb | fix | TestBed should still use the microtask queue to schedule effects (#53843) |
router
| Commit | Type | Description |
| -- | -- | -- |
| 5c1d441029 | feat | Add info property to NavigationExtras
(#53303) |
| 50d7916278 | feat | Add router configuration to resolve navigation promise on error (#48910) |
| a5a9b408e2 | feat | Add transient info to RouterLink input (#53784) |
| 726530a9af | feat | Allow onSameUrlNavigation: 'ignore'
in navigateByUrl
(#52265) |
<!-- CHANGELOG SPLIT MARKER -->
<a name="17.0.9"></a>