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.
19.0.0-next.7 (2024-09-25)
Breaking Changes
core
-
Changes to effect timing which generally has two implications:
-
effects which are triggered outside of change detection run as part of
the change detection process instead of as a microtask. Depending on the
specifics of application/test setup, this can result in them executing
earlier or later (or requiring additional test steps to trigger; see below
examples).
-
effects which are triggered during change detection (e.g. by input
signals) run earlier, before the component's template.
We've seen a few common failure cases:
-
Tests which used to rely on the Promise
timing of effects now need to
await whenStable()
or call .detectChanges()
in order for effects to
run.
-
Tests which use faked clocks may need to fast-forward/flush the clock to
cause effects to run.
-
effect()
s triggered during CD could rely on the application being fully
rendered (for example, they could easily read computed styles, etc). With
the change, they run before the component's updates and can get incorrect
answers. The recent afterRenderEffect()
API is a natural replacement for
this style of effect.
-
effect()
s which synchronize with the forms system are particularly
timing-sensitive and might need to adjust their initialization timing.
-
ExperimentalPendingTasks
has been renamed to
PendingTasks
.
core
| Commit | Type | Description |
| -- | -- | -- |
| fc59e2a7b7 | feat | change effect() execution timing & no-op allowSignalWrites
(#57874) |
| a7eff3ffaa | feat | mark signal-based query APIs as stable (#57921) |
| a1f229850a | feat | migrate ExperimentalPendingTasks to PendingTasks (#57533) |
| 950a5540f1 | fix | Ensure the ViewContext
is retained after closure minification (#57903) |
language-service
| Commit | Type | Description |
| -- | -- | -- |
| 7ecfd89592 | fix | The suppress diagnostics option should work for external templates (#57873) |
<!-- CHANGELOG SPLIT MARKER -->
<a name="18.2.6"></a>