Angular 6同级路由(用于边栏)

时间:2019-03-24 00:22:27

标签: angular angular2-routing angular-routing angular-template router-outlet

我正在尝试在我的Angular应用程序上实现路由功能,但部分成功。路由在我的应用程序的主容器上工作(路径,没有插座属性)。

我遇到的问题是,我正在尝试为侧边栏(带有名为“ sidebar”的出口属性的路径)实现第二个路由器出口。它不起作用。

我的路径如下:

    const appRoutes: Routes = [
  { path: '', component: WorkspaceAreaComponent }, // localhost:4200/workspace
  { path: 'workspace', component: WorkspaceAreaComponent }, // localhost:4200/workspace
  { path: 'reports', component: ReportsAreaComponent, children: [
      { path: '', component: ActiveProjectStatisticsComponent },
      { path: 'active-project-stats', component: ActiveProjectStatisticsComponent },
      { path: 'productivity-tracking', component: ProductivityTrackingComponent },
    ] },
  { path: 'templates', component: TemplatesAreaComponent, children: [
      { path: '', component: TemplateSectionsComponent },
      { path: 'email', component: TemplateEmailComponent }
    ] },
  { path: '', component: TemplatesComponent, outlet: 'sidebar' },
  { path: 'templates', component: TemplatesComponent, outlet: 'sidebar' },
  { path: 'reports', component: ReportsComponent, outlet: 'sidebar' }
];

主容器的组件如下:

<div class="mainContainer">
  <router-outlet></router-outlet>
</div>

侧边栏的组件如下所示:

<div class="asideBox">
  <router-outlet name="sidebar"></router-outlet>
</div>

包含它们的父组件模板如下:

<app-header></app-header>
<div class="bodyContainer">
  <div *ngIf="!isOrganiserPage; else organiserPane" class="flexResponsive768">
    <aside class="width20">
      <!-- SIDEBAR COMPONENT -->
      <app-sidebar></app-sidebar>
    </aside>
    <!-- MAIN COMPONENT -->
    <main class="width80" app-main-pane>

    </main>
  </div>
  <ng-template #organiserPane>
    <main></main>
  </ng-template>
</div>
<app-right-pane></app-right-pane>
<app-modals></app-modals>

有人可以帮我解决这个问题吗?我已经尝试过通过教程来解决此问题,但运气还不足。

谢谢。

1 个答案:

答案 0 :(得分:0)

我刚刚找到了自己问题的答案。

路径本身很好,需要更改路由器链接。

我对此进行了更改:

<div class="mainMenu flex">
      <a routerLink="organiser" class="item" [ngClass]="organiserTabActive ? 'active' : ''" (click)="onClick(1)">ORGANISER</a>
      <a routerLink="workspace" class="item" [ngClass]="workspaceTabActive ? 'active' : ''" (click)="onClick(2)">WORKSPACE</a>
      <a routerLink="reports" class="item" [ngClass]="reportsTabActive ? 'active' : ''" (click)="onClick(3)">REPORTS</a>
      <a routerLink="templates" class="item" [ngClass]="templatesTabActive ? 'active' : ''" (click)="onClick(4)">TEMPLATES</a>
    </div>

对此:

<div class="mainMenu flex">
      <a [routerLink]="[{ outlets: { sidebar: ['organiser'] } }]" class="item" [ngClass]="organiserTabActive ? 'active' : ''" (click)="onClick(1)">ORGANISER</a>
      <a [routerLink]="[{ outlets: { sidebar: ['workspace'] } }]" class="item" [ngClass]="workspaceTabActive ? 'active' : ''" (click)="onClick(2)">WORKSPACE</a>
      <a [routerLink]="[{ outlets: { sidebar: ['reports'] } }]" class="item" [ngClass]="reportsTabActive ? 'active' : ''" (click)="onClick(3)">REPORTS</a>
      <a [routerLink]="[{ outlets: { sidebar: ['templates'] } }]" class="item" [ngClass]="templatesTabActive ? 'active' : ''" (click)="onClick(4)">TEMPLATES</a>
    </div>

一切正常。除了我以外,没人感谢我!