【问题标题】:Angular2: Multiple router-outlet in Child ComponetAngular 2:子组件中的多个路由器出口
【发布时间】:2017-02-12 07:09:23
【问题描述】:

我正在创建 scotch.io 的 angular-ui-router tutorial 的 ng2 版本。 我能够实现homeabout 页面的基本导航。在home 中,我可以为儿童创建导航,即listparagraph。我被about 页面卡住了。我可以导航到关于页面,但我无法在其相应的网点中呈现about 的孩子component。这就是我正在做的事情:

about.component.html

<div class="jumbotron text-center">
    <h1>The About Page</h1>
    <p>This page demonstrates <span class="text-danger">multiple</span> and <span class="text-danger">named</span> views.</p>
</div>
<div class="row">
    <div class="col-sm-6">
        <router-outlet name="aboutOne"></router-outlet>
        <router-outlet name="aboutTwo"></router-outlet>
    </div>
</div>

about.routes.ts

export const aboutRoutes: Routes = [
    {
        path: 'about',
        component: AboutComponent,
        children: [
            {
                path: 'about',
                outlet: 'aboutOne',
                component: AboutOneComponent
            },
            {
                path: 'about',
                outlet: 'aboutTwo',
                component: AboutTwoComponent
            }
        ]
    }
];

about-routing.module.ts

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

import { aboutRoutes } from './about.routes';
@NgModule({
    imports: [RouterModule.forChild(aboutRoutes)],
    exports: [RouterModule]
})
export class AboutRoutingModule {

}

我不知道我错过了什么。关于页面正在呈现,但它没有呈现它的子组件,控制台中也没有错误。这是完整的github repo

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    在玩了名为&lt;router-outlet&gt; 之后,我想通了。我没有给出正确的路径。因为当用户登陆about 页面时,我的两个组件都应该在其相应的插座中呈现。两者都没有相对的网址/路径。它们是about 的默认子代。所以我将我的path:'about' 更新为path:''。它开始工作了。希望这对某人有所帮助。

    about.routes.ts

    export const aboutRoutes: Routes = [
        {
            path: 'about',
            component: AboutComponent,
            children: [
                {
                    path: '',
                    outlet: 'aboutOne',
                    component: AboutOneComponent,
                    pathMatch: 'full'
                },
                {
                    path: '',
                    outlet: 'aboutTwo',
                    component: AboutTwoComponent,
                    pathMatch: 'full'
                }
            ]
        }
    ];
    

    @Pradeep 感谢您强调path 问题。

    【讨论】:

    • 谢谢。我花了一整天的时间来弄清楚。
    【解决方案2】:

    嘿@hitesh 很可能错误在于关于组件的路由,您对两个组件以及父组件都使用了相同的路径名称,请更改它并像这样尝试

    export const aboutRoutes: Routes = [
        {
            path: 'about',
            component: AboutComponent,
            children: [
                {
                    path: 'aboutOne',
                    outlet: 'aboutOne',
                    component: AboutOneComponent
                },
                {
                    path: 'aboutTwo',
                    outlet: 'aboutTwo',
                    component: AboutTwoComponent
                }
            ]
        }
    ];
    

    可能是这样的情况,Angular 会尝试根据 URL 加载组件,在每种情况下都找到 about,因此在这种情况下子组件可能不会呈现。

    【讨论】:

    • 感谢您的回答。问题是aboutOneaboutTwo 都不是路由,而是视图,您可以查看 scotch.io 教程。 embed.plnkr.co/IzimSVsstarlFviAm7S7/preview 它只有两个 ui-view 并且路由模板落入这些视图中。我想使用路由器插座做类似的事情。我的组件在同一条路线中进入相应的出口
    • 能否请您在一些 plunker 上重现您的问题??
    • 我有回购代码。 github.com/hk-skit/angular2routing你可以去看看。我已经使用 angular-cli 创建了 repo。所以把它移到 plunkr 会很耗时。
    • 哦,对不起兄弟,即使我没有太多时间让你的回购运行,但你提供的 plunker 似乎来自 angular1.x embed.plnkr.co/IzimSVsstarlFviAm7S7/preview
    • 酷。我钻研网点并试图弄清楚。
    猜你喜欢
    • 2017-08-08
    • 2017-12-23
    • 1970-01-01
    • 2017-02-18
    • 2022-01-01
    • 1970-01-01
    • 2017-07-02
    • 2017-12-03
    • 2017-08-21
    相关资源
    最近更新 更多