如果Angular路由没有重定向到正确的组件,可能有以下几种解决方法:
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
// ...
];
const routes: Routes = [
{ path: 'about', component: AboutComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
// ...
];
Home
About
import { Router } from '@angular/router';
export class MyComponent {
constructor(private router: Router) {}
navigateToHome() {
this.router.navigate(['/home']);
}
navigateToAbout() {
this.router.navigate(['/about']);
}
}
import { RouterModule } from '@angular/router';
@NgModule({
imports: [RouterModule.forRoot(routes)],
// ...
})
export class AppModule { }
通过以上方法逐一排查,可以找到并解决Angular路由没有重定向到正确组件的问题。
上一篇:Angular路由没有被触发问题
下一篇:Angular路由模块