【问题标题】:Creating conditional partials with ngIf使用 ngIf 创建条件部分
【发布时间】:2018-11-18 02:43:56
【问题描述】:

我希望通过条件逻辑实现应用程序范围的导航和子导航组合,以在特定页面上显示侧导航。我在这里尝试了 routerLink 帖子上的一些 ngIf,但没有一个解决了我的问题。 如 Clarity 文档所述,app.component.html 是我的主要容器、标题和子导航。 app.component.html

<div class="main-container">
   <header class="header">
   </header>

   <nav class="subnav">
   </nav>

   <div class="content-container">
      <div class="content-area">
         <router-outlet></router-outlet>
      </div>

      <nav *ngIf="router.url === '/page'" class="sidenav">
      </nav>
   </div>
</div>

app.component.ts

class PageComponent {
    constructor(public router: Router) {

    }
}

【问题讨论】:

  • 你可以在app.component.ts 中添加代码吗?
  • 这是第二个代码块
  • 另外,您可以使用 *ngIf 或 ngStyle 或 canActivate 添加命名路由器的组合。这绝对是更清洁和您偏好的选择。我已经将 *ngIf 用于某些需要隐藏的菜单项,如果这是目的的话。

标签: angular vmware-clarity


【解决方案1】:

您的应用程序中可以有两种不同的布局

  1. Blank Layout //没有侧边栏
  2. 主布局 //它有你的侧边栏

您的router 将如下所示

const routes: Routes = [
 {
    path: '',
    component: BlankLayoutComponent,
    children: [
        { path: '', redirectTo: 'logincustomer',pathMatch:'full' },
        { path: 'logincustomer', component: LoginComponent }
    ]
  },
{

    path: 'user',
    component: MainLayoutComponent,
    canActivate: [AuthGuard],
    children: [
         { path: 'user', redirectTo:'dashboard', pathMatch: 'full', canActivate: [AuthGuard] }]
 };

【讨论】:

  • 太棒了,谢谢!我可以更简单地使用 *ngStyle 使用 display: none 属性来解决这个问题吗?
  • @JonathonHambleton 你也可以使用ngStyle 来实现,但是上面显示的方式对于大规模应用非常有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 2022-07-07
相关资源
最近更新 更多