在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中常用的路由管理方法,通过这些方法可以实现对路由的配置、导航和参数获取等操作。