Angular 2 Routing Lazy Loading - 路由中的参数

Adr*_*ien 5 parameters router angular

我有一个延迟加载的组件和一个参数的问题。我有两个模块:一个仪表板模块 (DashboardModule) 带有一个仪表板组件 (DashboardComponent),另一个名为 user (UserModule) 的模块带有一个组件 (UserComponent),例如。UserComponent 用于显示一个用户或创建一个新用户。我想延迟加载模块 UserModule。所以我在文件 app.routing.ts 中的配置是:

export const appRoutes: Routes =  [
    { path: '',   redirectTo: '/dashboard', pathMatch: 'full' },
    { path: '**', redirectTo: '/dashboard', pathMatch: 'full' },
    { path: 'user', loadChildren: 'app/user/user.module#UserModule' },
]

@NgModule({
  imports: [ RouterModule.forRoot(appRoutes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}
Run Code Online (Sandbox Code Playgroud)

现在,在我的模块 User 中,我有一个文件来配置我的路由:user.routing.ts。我想为我的两种情况使用相同的路线:如果我的路线中有一个 id,则显示一个用户,或者如果 id 为空,则创建一个用户。所以我尝试这个,但它不起作用:

const userRoutes: Routes = [
    { path: '', component: UserComponent },
    { path: ':id', component: UserComponent } // This line does not work
];

@NgModule({
    imports: [RouterModule.forChild(userRoutes)],
    exports: [RouterModule]
})
export class UserRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

如果我的路线有一个 Id,它就不起作用,并且路线会自动返回给用户。例如: locahost:4200/user/123 不起作用,并且重定向到 localhost:4200/user

我没有找到这个案例的样本,所以我希望有人能帮助我。

谢谢你。

Ale*_*kij -1

你应该删除

{ path: '**', redirectTo: '/dashboard', pathMatch: 'full' },

从appRoutes设置