angular4 - 访问使用参数的路由的子路由

时间:2017-11-07 19:35:09

标签: angular angular-routing

我的路线看起来像这样:

const routes: Routes = [
  { path: '', redirectTo: '/login', pathMatch: 'full' },
  { path: 'login',  component: LoginComponent },
  { path: 'home',  component: HomeComponent,
    children:[
              { path: '', redirectTo: 'cat', pathMatch: 'full' },
              { path: 'cat',  component: CatComponent},
              { path: 'dog',  component: DogComponent},
              { path: 'vet/:id',  component: VetComponent,
                children:[
                          { path: '', redirectTo: 'hospital', pathMatch: 'full' },
                          { path: 'hospital',  component: HospitalComponent},
                          { path: 'clinic',  component: ClinicComponent}
                ]}
    ]},
  { path: '**', redirectTo: '/login', pathMatch: 'full' },
];

然后我试图访问诊所路径,所以:

/home/vet/9999/clinic

所以我尝试了以下内容:

this.router.navigate(['/home/vet/clinic', 9999]);
this.router.navigate(['/home/vet/:id/clinic', 9999]);

但我一直被重定向到登录页面。

我需要能够进入诊所页面并且还可以访问id变量。我不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

我找到了答案。你打电话的时候需要看起来像这样:

this.router.navigate(['/home/vet', 9999, 'clinic']);

而不是

this.router.navigate(['/home/vet/clinic', 9999]);