在Angular中,路由管理是通過Angular Router模塊來實現的。以下是一些常用的路由管理方法:
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppModule { }
<nav>
<a routerLink="/home">Home</a>
<a routerLink="/about">About</a>
</nav>
import { Router } from '@angular/router';
export class MyComponent {
constructor(private router: Router) {}
goToAbout() {
this.router.navigate(['/about']);
}
}
import { ActivatedRoute } from '@angular/router';
export class MyComponent {
constructor(private route: ActivatedRoute) {
this.route.params.subscribe(params => {
console.log(params);
});
}
}
這些是一些Angular中常用的路由管理方法,通過這些方法可以實現對路由的配置、導航和參數獲取等操作。