导航到根路由"/"

koe*_*ech 8 typescript angular2-routing angular

我试图从儿童路线导航到回家路线但没有任何反应.

即目前的路线是 /projects

既不工作this.router.navigate(['/'])也不routerLink="/"工作.

routerConfig看起来像这样

const routes: Routes = [
 {
   path: '',
   component: HomeComponent
 },
 {
   path: '/projects',
   component: ProjectsComponent
 }
]
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

wun*_*uno 11

尝试添加redirectTo路由到您的路由配置:

const routes: Routes = [
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    { path: 'home', component: HomeComponent },
    { path: 'projects', component: ProjectsComponent }
];
Run Code Online (Sandbox Code Playgroud)

  • 它与子组件有关。例如,我认为如果您的家庭组件。实际上是在 app.component 旁边而不是在 /components/home.component 我认为这没有必要。老实说,我不太确定,但我自己花了 6 个小时。所以我感受到你的痛苦兄弟我感受到你的痛苦。 (4认同)