【发布时间】:2017-12-13 14:41:10
【问题描述】:
如果路由器位于/client 和/product 相同,我有一个正在加载的DashboardComponent:
export const routes: Routes =
[
{ path: '', component: HomeComponent },
{ path: 'client', component: DashboardComponent },
{ path: 'product', component: DashboardComponent }
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class RoutesModule { }
然后我像这样(dashboard.component.ts)加载不同的组件:
this.router.events.subscribe(path => {
if (this.router.url == "/client") {
this.isClient = true;
}
if (this.router.url != "/client") {
this.isClient = false;
}
if (this.router.url == "/product") {
this.isProduct = true;
}
if (this.router.url != "/product") {
this.isProduct = false;
}
});
然后在dashboard.component.html我做:
<app-client *ngIf="isClient"></app-client>
<app-product *ngIf="isProduct"></app-product>
我觉得这不是这样做的方法,但如果你去例如,我找不到一个很好的解释来说明如何实现它。 /dashboard/client 它将 ClientComponent 加载到 DashboardComponent 内部,ProductComponent 也是如此
【问题讨论】: