简体   繁体   English

Angular2 嵌套路由不起作用

[英]Angular2 nested routing not working

I am working on a small trial project with Angular2 where I have a menu and a sub-menu .我正在使用 Angular2 进行一个小型试验项目,其中有一个菜单和一个子菜单 Hence I need to have some kind of nested routing for sub-menu.因此,我需要为子菜单设置某种嵌套路由。 I referred to angular.io site in the case study: Tour of heroes section but was not able to implement it successfully.我在案例研究中提到了 angular.io 站点:英雄之旅部分,但未能成功实施。

My main menu(navigation) has this routing configuration我的主菜单(导航)有这个路由配置

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

used in the UI(template) as在 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>

Letters section will have a sub-navigation and hence child routes.字母部分将有一个子导航,因此有子路线。

sub-menu(sub-navigation) under letters component has been configured as字母组件下的子菜单(子导航)已配置为

@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' }
])

used in the UI(template) as在 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>

The main menu works and displays the corresponding template except for letters section.主菜单工作并显示除字母部分之外的相应模板。 When I click on letters option, the corresponding template of sub-menu is loaded but in the address bar I still see http://localhost:3000/contact which I had navigated to before, I get the following error in my console, and thereafter nothing works.当我单击字母选项时,会加载相应的子菜单模板,但在地址栏中我仍然看到之前导航到的http://localhost:3000/contact ,我在控制台中收到以下错误,并且此后没有任何效果。

angular2.dev.js:23877 Error: Component "AppComponent" has no route named "All". angular2.dev.js:23877 错误:组件“AppComponent”没有名为“All”的路由。

I tried a lot of things that people suggested for similar problems on github and stackoverflow but nothing seems to work.我尝试了很多人们针对 github 和 stackoverflow 上的类似问题提出的建议,但似乎没有任何效果。

Is nested routing really supported in angular2? angular2 真的支持嵌套路由吗? I went through this thread ( Router Huge Flaw - Does not allow more than 1 level of nesting #6204 ) on github but didn't help.我在 github 上浏览了这个线程(路由器巨大缺陷 - 不允许超过 1 级嵌套 #6204 )但没有帮助。 Is there any way to get around this problem?有没有办法解决这个问题?

Considering comments on the questions, the solution is:考虑到对问题的评论,解决方案是:

  1. Removed providers: [ROUTER_PROVIDERS] from the child component LetterComponent which has sub-routes.从具有子路由的子组件LetterComponent删除了providers: [ROUTER_PROVIDERS]
  2. Changed改变了

    <router-outlet name="letter-outlet"></router-outlet>

    to

    <router-outlet></router-outlet>

Hence the final structure goes like this:因此最终的结构是这样的:

AppComponent:应用组件:

@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 (child component that has sub-routes): 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' }
])

Answering my own question, as somebody with same problem might find useful回答我自己的问题,因为有同样问题的人可能会觉得有用

In fact when using routing you need to have route definitions in the component that you bootstrap for your application.事实上,在使用路由时,您需要在为应用程序引导的组件中定义路由。 There is no problem to define sub routes in other components.在其他组件中定义子路由没有问题。

See this question for more details:有关更多详细信息,请参阅此问题:

Edit编辑

From your error, you forgot to define a route named All in the route definition of your AppComponent class.由于您的错误,您忘记在AppComponent类的路由定义中定义名为All的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM