主路由器插座内的路由器插座不会显示任何错误

时间:2020-02-10 14:38:37

标签: javascript angular router-outlet

由于我没有从Angular收到任何错误,因此似乎无法找到问题所在。

我正在尝试在主路由器插座内放置一个路由器插座,以显示组件。我正确选择了路由器,但没有显示我在第二个插座中选择的组件。

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';


const routes: Routes = [
    { path: 'dashboard', loadChildren: () => import('./pages/dashboard/dashboard.module').then(m => m.DashboardModule) }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule { }

dashboard-routing.module.ts

// ...

const routes: Routes = [
    { path: '', component: DashboardComponent },
    { path: 'forms', component: FormsComponent, outlet: 'sidebar' },
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class DashboardRoutingModule { }

dashboard.component.html

<app-header></app-header>

<mat-sidenav-container>
    <mat-sidenav mode="side" opened>
        <mat-nav-list>
            <a mat-list-item [routerLink]="[{ outlets: { sidebar: ['forms'] } }]" routerLinkActive="active">Forms</a>
        </mat-nav-list>
    </mat-sidenav>
    <mat-sidenav-content>
        <main class="bfa-main">
            <router-outlet name="sidebar"></router-outlet>
        </main>
    </mat-sidenav-content>
</mat-sidenav-container>

<app-footer></app-footer>

正确选择了routerlink,并显示了活动的类和链接:

http://localhost:4200/dashboard/(sidebar:forms)

但是,当我希望看到内部路由器出口中名为sidebar的表单组件时,这里什么也没有显示且没有错误

有人知道我在这里想念什么吗?

谢谢

1 个答案:

答案 0 :(得分:0)

决定使用ng-content将仪表板作为包装,而不是将每个仪表板组件作为包装

<app-header></app-header>

<mat-sidenav-container>
    <mat-sidenav mode="side" opened>
        <mat-nav-list>
            <a mat-list-item [routerLink]="['forms']" routerLinkActive="active">Forms</a>
            <a mat-list-item [routerLink]="['users']" routerLinkActive="active">Users</a>
            <a mat-list-item [routerLink]="['posts']" routerLinkActive="active">Posts</a>
            <a mat-list-item [routerLink]="['pages']" routerLinkActive="active">Pages</a>
        </mat-nav-list>
    </mat-sidenav>
    <mat-sidenav-content>
        <main class="bfa-main">
            <ng-content></ng-content>
        </main>
    </mat-sidenav-content>
</mat-sidenav-container>

<app-footer></app-footer>