【问题标题】:Enable Component based on the route url根据路由 url 启用组件
【发布时间】:2017-07-30 17:54:03
【问题描述】:

如何根据路由 url 启用特定组件,

我有一个组件如下,它位于母版页中

app.component.html

<div class="col-md-7">
 <headerBar  ></headerBar>
</div>
<div class="col-md-4">
   <fd-login></fd-login>
</div>

此状态下app url显示如下,

http://localhost:3000/en/search

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

【问题讨论】:

标签: angular typescript


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    @GünterZöchbauer 想说的是,你需要这样的东西。

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

    <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},
    ];
    

    然后将它与您的应用组件一起导入到您的主模块中。

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

    请继续访问 Angular 网站,因为他们的解释比我在这里展示的更详细。他们有一个您可以阅读的教程,以及代码,以便了解路由的工作原理。

    这里是 Angular 2 架构的链接:https://angular.io/docs/ts/latest/guide/architecture.html

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 2020-08-09
      • 2016-04-29
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2019-12-17
      相关资源
      最近更新 更多