【问题标题】:Angular2 nested routing not workingAngular2嵌套路由不起作用
【发布时间】:2016-07-28 18:43:10
【问题描述】:

我正在使用 Angular2 进行一个小型试验项目,其中有一个 菜单 和一个 子菜单。因此,我需要为子菜单设置某种嵌套路由。我在案例研究:英雄之旅部分提到了 angular.io 网站,但未能成功实施。

我的主菜单(导航)有这个路由配置

@RouteConfig([
    { path: '/letter/...', name: 'Letter', component: LetterComponent },
    { path: '/contact', name: 'Contact', component: ContactsComponent },
    { path: '/notes', name: 'Notes', component: NotesComponent }
])

在 UI(模板)中用作

<ul class="sidebar-nav">
    <li id="menu-title-li">
         <div id="menu-title" class="noselect">MENU</div>
    </li>
    <li>
        <a [routerLink]="['Contacts']">Contacts</a>
    </li>
    <li>
        <a [routerLink]="['/Letter','All']">Letters</a>
    </li>
    <li>
        <a [routerLink]="['Notes']">Notes</a>
    </li>
</ul>
<router-outlet></router-outlet>

字母部分将有一个子导航,因此有子路线。

字母组件下的子菜单(子导航)已配置为

@Component({
    selector: 'letter',
    templateUrl: './app/Letter/template.html',
    directives: [RouterOutlet, ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})

@RouteConfig([
    { path: '/all', component: AlllettersComponent, name: 'All' , useAsDefault: true},
    { path: '/inwards', component: InwardsComponent, name: 'Inwards' },
    { path: '/outwards', component: OutwardsComponent, name: 'Outwards' }
])

在 UI(模板)中用作

<ul class="nav nav-tabs">
    <li role="presentation">
        <a [routerLink]="['All']">All</a>
    </li>
    <li role="presentation">
        <a [routerLink]="['Inwards']">Inwards</a>
    </li>
    <li role="presentation">
        <a [routerLink]="['Outwards']">Outwards</a>
    </li>
</ul>
<router-outlet name="letter-outlet"></router-outlet>

主菜单工作并显示除字母部分之外的相应模板。当我点击字母选项时,子菜单的相应模板已加载,但在地址栏中我仍然看到我之前导航到的http://localhost:3000/contact,我在控制台中收到以下错误,此后没有任何效果。

angular2.dev.js:23877 错误:组件“AppComponent”没有名为“All”的路由。

我尝试了很多人们在 github 和 * 上针对类似问题提出的建议,但似乎没有任何效果。

angular2 真的支持嵌套路由吗?我在 github 上浏览了这个线程 (Router Huge Flaw - Does not allow more than 1 level of nesting #6204) 但没有帮助。有什么办法可以解决这个问题吗?

【问题讨论】:

  • 您可以尝试从子组件中删除providers: [ROUTER_PROVIDERS] 吗?
  • 哦!那做了一些事情..我能够浏览我的子路由,但是 1) 子路由的模板没有被加载 2) 当我尝试导航到主路由(级别 1)时,它会抛出一个 ERROR 错误:未捕获(在promise): registerPrimaryOutlet 期望被一个未命名的出口调用。
  • 好,你能把&lt;router-outlet name="letter-outlet"&gt;&lt;/router-outlet&gt;也改成&lt;router-outlet&gt;&lt;/router-outlet&gt;吗?
  • 哇!它有效!完美的!我挣扎了将近 2 天.. 请将其发布为答案.. 对很多人都有帮助,因为我觉得很多人都在为此苦苦挣扎。

标签: typescript angular


【解决方案1】:

考虑到cmets上的问题,解决方法是:

  1. 从具有子路由的子组件 LetterComponent 中删除了 providers: [ROUTER_PROVIDERS]
  2. 改变了

    &lt;router-outlet name="letter-outlet"&gt;&lt;/router-outlet&gt;

    &lt;router-outlet&gt;&lt;/router-outlet&gt;

因此最终的结构是这样的:

应用组件:

@Component({
    selector: 'my-app',
    templateUrl: `
        <ul class="sidebar-nav">
            <li id="menu-title-li">
                <div id="menu-title" class="noselect">MENU</div>
            </li>
            <li>
                <a [routerLink]="['Contacts']">Contacts</a>
            </li>
            <li>
                <a [routerLink]="['/Letter','All']">Letters</a>
            </li>
            <li>
                <a [routerLink]="['Notes']">Notes</a>
            </li>
        </ul>
        <router-outlet></router-outlet>
    `,
    directives: [
        ROUTER_DIRECTIVES
    ],
    providers: [
        ROUTER_PROVIDERS
    ]
})

@RouteConfig([
    { path: '/letter/...', name: 'Letter', component: LetterComponent },
    { path: '/contact', name: 'Contact', component: ContactsComponent },
    { path: '/notes', name: 'Notes', component: NotesComponent }
])

LetterComponent(有子路由的子组件):

@Component({
    selector: 'letter',
    templateUrl: `
        <ul class="nav nav-tabs">
            <li role="presentation">
                <a [routerLink]="['All']">All</a>
            </li>
            <li role="presentation">
                <a [routerLink]="['Inwards']">Inwards</a>
            </li>
            <li role="presentation">
                <a [routerLink]="['Outwards']">Outwards</a>
            </li>
        </ul>
        <router-outlet></router-outlet>
    `,
    directives: [RouterOutlet, ROUTER_DIRECTIVES]
})

@RouteConfig([
    { path: '/all', component: AlllettersComponent, name: 'All' , useAsDefault: true},
    { path: '/inwards', component: InwardsComponent, name: 'Inwards' },
    { path: '/outwards', component: OutwardsComponent, name: 'Outwards' }
])

回答我自己的问题,遇到同样问题的人可能会觉得有用

【讨论】:

    【解决方案2】:

    事实上,在使用路由时,您需要在为应用程序引导的组件中定义路由。在其他组件中定义子路由没有问题。

    查看这个问题了解更多详情:

    编辑

    由于您的错误,您忘记在 AppComponent 类的路由定义中定义名为 All 的路由。

    【讨论】:

    • 啊!这对初学者来说很困惑。您能在这里解释并提供解决方案吗?对其他人也有帮助
    • 其实没那么明显!我会尝试将您的第一个 @RouteConfig 移动到您的 AppComponent 班级...
    • 我的主导航路由定义在我为我的应用程序引导的组件中(AppComponent),但子路由在其他组件中(Letter)。你说没有问题。但我的项目不起作用
    • 抱歉。我没有正确地理解你的错误。从中,我认为您忘记在 AppComponent 类的路由定义中定义一个名为 All 的路由。
    • 为什么搞得这么复杂?有些人甚至在其他论坛上取笑这个
    最近更新 更多