相对路径角度不起作用

时间:2018-08-07 20:38:45

标签: angular typescript

我有一个功能模块,我正在关注文档。

我的危机路由模块看起来像这样。

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CrisisCenterComponent } from './crisis-center.component';
import { CrisisListComponent } from './crisis-list/crisis-list.component';
import { CrisisDetailComponent } from './crisis-detail/crisis-detail.component';
import { CrisisCenterHomeComponent } from './crisis-center-home/crisis-center-home.component';

const routes: Routes = [
  {
    path: 'crisis-center',
    component: CrisisCenterComponent,
    children: [
      {
        path: '',
        component: CrisisListComponent,
        children: [
          {
            path: ':id',
            component: CrisisDetailComponent
          },
          {
            path: '',
            component: CrisisCenterHomeComponent
          }
        ]
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class CrisisCenterRoutingModule { }

我想我可以通过以下方式访问ID:

 [routerLink]="['./crisis-center/..', crisis.id]"

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

我发现Angular有一种构造具有子页面的参数的首选方法。我会这样声明路线:

children: [
      {
        path: ':id',
        component: CrisisListComponent,
        children: [
          {
            path: 'crisis-home',
            component: CrisisCenterHomeComponent
          }
        ]
      }
    ]

然后您可以使用以下方法访问路线:

[routerLink]="['./crisis-center/..', crisis.id, 'crisis-home']"

哪个会给你:

localhost/crisis-center/1/crisis-home

答案 1 :(得分:0)

我知道它可以工作,但是我不能和嵌套的孩子一起工作。

我的新路线现在是:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CrisisCenterComponent } from './crisis-center.component';
import { CrisisListComponent } from './crisis-list/crisis-list.component';
import { CrisisDetailComponent } from './crisis-detail/crisis-detail.component';
import { CrisisCenterHomeComponent } from './crisis-center-home/crisis-center-home.component';
import { CrisisDetailedComponent } from './crisis-detailed/crisis-detailed.component';

const crisisRoutes: Routes = [
  {
    path: 'crisis-center',
    component: CrisisCenterComponent,
    children: [
      {
        path: '',
        component: CrisisListComponent,
        // children: [
        //   { path: '', component: CrisisCenterHomeComponent },
        //   { path: 'crisis/:id', component: CrisisDetailedComponent },
        // ]
      },
      { path: ':id', component: CrisisDetailedComponent },
      { path: 'home', component: CrisisCenterHomeComponent },
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(crisisRoutes)],
  exports: [RouterModule]
})
export class CrisisCenterRoutingModule { }

我不知道为什么它只接受一条嵌套路线,也许我也不知道要访问路线。