Angular-域之后的动态路由,例如example.com/:id?

时间:2019-03-29 11:45:54

标签: angular

我想在域之后立即获取用户ID,例如example.com/user1(example.com/:id),而不是example.com/user/user1(example.com/user/:id)。那怎么可能?以下是我的路由模块。我想删除所有这些页面路径,然后放在:id那里。因此它是动态的。页面在我们的代码中仅表示“用户”。

我对最简单的解决方案感到非常满意。

亲切的问候, 托马斯

const routes: Routes = [
  { path: 'homepage', component: HomepageComponent },
  { path: 'register', component: RegisterComponent },
  {
    path: '', component: MainComponent, children:
      [
        { path: 'search', component: SearchComponent },
        { path: 'menu', component: MenuComponent },
        {
          path: 'page',
          component: TabsComponent,
          children: [
            { path: 'photos', component: PhotoListComponent },
            { path: 'videos', component: VideoListComponent }
          ]
        },
        { path: 'page/photos/:id', component: PhotoDetailComponent },
        { path: 'page/videos/:id', component: VideoDetailComponent },
      ]
  },
  { path: 'page/add/photo', component: PhotoAddComponent},
  { path: 'page/add/video', component: VideoAddComponent}
];

1 个答案:

答案 0 :(得分:0)

以下是动态路由的有效演示,该动态路由从域名之后开始,没有任何前缀路由:

https://stackblitz.com/edit/angular-dynamic-route-test