Angular 基于路由url启用组件

Angular 基于路由url启用组件,angular,typescript,Angular,Typescript,如何基于路由url启用特定组件 我在母版页中有一个组件,如下所示 app.component.html 我想隐藏标题栏,使其仅在应用程序处于上述状态以外的其他状态时显示,如何使用ngIf在html中检查它,或者是否有其他方法?您可以订阅路由器事件,然后根据URL设置属性: constructor(router:Router) { router.events .filter(e => e instanceof NavigationEnd) .forEach(e => con

如何基于路由url启用特定组件

我在母版页中有一个组件,如下所示

app.component.html


我想隐藏标题栏,使其仅在应用程序处于上述状态以外的其他状态时显示,如何使用ngIf在html中检查它,或者是否有其他方法?

您可以订阅路由器事件,然后根据URL设置属性:

constructor(router:Router) {
  router.events
  .filter(e => e instanceof NavigationEnd)
  .forEach(e => console.log(e.url);  
}

@甘特斯切鲍尔试图说,你需要这样的东西

创建主应用程序组件后,使用路由器出口将html文件链接到该组件

<div class="container">
<router-outlet></router-outlet>
</div>
然后将其与应用程序组件一起导入主模块

这将允许您将1个组件映射到1个状态,从而获得不显示其他组件的所需结果

请转到Angular网站,因为他们对它的解释比我在这里展示的更详细。他们有一个教程,你可以阅读,代码一起学习如何路由工程


这里是Angular 2架构的链接:

路由器通常就是这么做的。@GünterZöchbauer我不明白问题出在哪里?这么多反对票
constructor(router:Router) {
  router.events
  .filter(e => e instanceof NavigationEnd)
  .forEach(e => console.log(e.url);  
}
<div class="container">
<router-outlet></router-outlet>
</div>
export const rootRouterConfig = [
  {path: '', redirectTo: 'home', pathMatch: 'full'},
  {path: 'home', component: HomeSectionComponent},
  {path: 'search', component: SearchSectionComponent},
  {path: 'login', component: LoginSectionComponent},
];