【问题标题】:Populating a child router-outlet Angular2填充子路由器出口Angular2
【发布时间】:2017-07-25 12:42:52
【问题描述】:

因为我听说 Angular 的路由器支持嵌套的 标签,所以我尝试使用其中的两个。我正在使用最新的 Angular-CLI,从 ui-router 转换为 Angular 的路由器。我无法获得第二个路由器出口来填充内容。

(父路由工作正常) app-routing.module.ts

...
const routes: Routes = [
    { path: '', pathMatch: 'full', component: LoginComponent },
    { path: 'login', pathMatch: 'full', component: LoginComponent },
    { path: 'dashboard',
        pathMatch: 'full',
        component: DashboardComponent // this holds the second router-outlet
    },
    { path: '**', component: LoginComponent }
];

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

app.component.ts - 这只是包含路由器插座,并且在顶层可以正常工作......

<router-outlet></router-outlet>

仪表板包含通用页眉、页脚、侧边栏等。这就是我希望它在顶级路由器插座中的原因。子路由器出口将填充子视图,但不填充。

尝试 1 dashboard-routing.module.ts

export const dashboardRoutes: Routes = [
    { path: '', pathMatch: 'full', redirectTo: 'home' },
    { path: 'home', pathMatch: 'full', component: HomeComponent },
    { path: 'about', pathMatch: 'full', component: AboutComponent }
]
@NgModule({
    imports: [
        RouterModule.forChild(dashboardRoutes)
    ],
    exports: [ RouterModule ]
})
export class DashboardRoutingModule { }

按照Angular2 : Multiple Router-Outlets & Router-Outlets Inside Child Route尝试2个dashboard-routing.module.ts

export const dashboardRoutes: Routes = [
    {
        path: 'dashboard',
        children:[
            { path: '', component: DashboardComponent},
            { path: 'home', component: HomeComponent},
            { path: 'about', component: AboutComponent}
        ]
    }
 ]
@NgModule({
    imports: [
        RouterModule.forChild(dashboardRoutes)
    ],
    exports: [ RouterModule ]
})
export class DashboardRoutingModule { }

在仪表板模板中,不会填充此嵌套的路由器插座。取而代之的是 app.component.html 中的顶级路由器插座 :(

dashboard.component.html

<header>...</header>
<aside class="sidenav">...<aside>

<!-- why can't I populate you? -->
<router-outlet></router-outlet>

****************** 回答,非常感谢PierreDuc! **************

app-routing.module.ts

// seems counter-intuitive that the dashboard component isn't actually in here..., but it works!
const routes: Routes = [
    { path: '', pathMatch: 'full', component: LoginComponent },
    { path: 'login', pathMatch: 'full', component: LoginComponent },
    { path: '**', component: LoginComponent }
];
@NgModule({
    imports: [
        RouterModule.forRoot(routes),
        DashboardRoutingModule // this is the magic. I'm assuming to put it second is better (though putting it first didn't seem to have any immediate negative effect)
    ],
    exports: [ RouterModule ]
})
export class AppRoutingModule { }

dashboard-routing.module.ts

export const dashboardRoutes: Routes = [
    {
        path: 'dashboard',
        component: DashboardComponent,
        children:[
            { path: '', component: HomeComponent },
            { path: 'home', pathMatch: 'full', component: HomeComponent },
            { path: 'about', pathMatch: 'full', component: AboutComponent }
        ]
    }
]
@NgModule({
    imports: [
        RouterModule.forChild(dashboardRoutes)
    ],
    exports: [ RouterModule ]
})
export class DashboardRoutingModule { }

以下是从根目录导航的方法:

login.component.ts

... passed validation ...
this._router.navigate(['/dashboard']);
this._router.navigate(['/dashboard/home']);

或通过仪表板中的侧边栏通过 routerLink 路由 仪表板.component.html

[routerLink]="['../login']" <!-- back to login, though the '../' seems less than ideal

或在仪表板视图中通过 routerLink 到子路由器插座: 仪表板.component.html:

[routerLink]="['../about']"

【问题讨论】:

    标签: angular angular2-routing router angular2-template


    【解决方案1】:

    router-outlets 在路由器配置中填充children 数组。但是有重复发生。您应该删除 AppRoutingModule 中的 dashboard 条目,然后尝试 2:

    应用程序路由

    const routes: Routes = [
        { path: '', pathMatch: 'full', component: LoginComponent },
        { path: 'login', pathMatch: 'full', component: LoginComponent },
        { path: '**', component: LoginComponent }
    ];
    

    在尝试 2 中保持您的 DashboardRoutingModule 原样(使用 children 数组),并在 AppRoutingModuleAppModule 中导入此模块。

    【讨论】:

    • 哇!做到了!我将在我的问题下方添加代码
    • 太棒了!非常感谢!现在你可能知道有一个轻微的非理想副作用——通过 .navigate 或 routerLink 导航需要我使用“../”返回仪表板,然后返回路线。你知道如何设置它,这样我就不必使用相对路径遍历,而只需导航到路由名称?谢谢!
    • 我相信除了使用相对路径之外没有其他办法。我不完全确定,但我想你也可以尝试导航到/dashboard。之所以这样,是因为这将为子路由提供与父路由同名的空间:dashboard/client/dashboard。现在,如果您在client,您将如何毫无歧义地导航到dashboard :)?
    猜你喜欢
    • 2016-11-25
    • 2017-10-27
    • 2016-12-09
    • 2017-02-15
    • 2016-09-20
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多