如何在角度2中路由多个组件

时间:2016-11-03 08:44:09

标签: angular angular2-routing angular2-template

我在角度2中有一个消息传递项目。 angular 2 routing image

路由条件:

1) path: /login , component: LoginComponent
2) path: /inbox , component: InboxMessageComponent
3) path:/inbox/all , "Load InboxMessageListComponent in Right side Box Only",
4) path:/inbox/13 , "Load InboxMessageDetailComponent in Right side Box Only"

所以我创建了两个名为app-routing.module.ts和inbox-routing.module.ts的路由模块。

应用-routing.module.ts

@NgModule({
    imports: [
        RouterModule.forRoot([
            { path: 'login', component: LoginComponent},
            { path: 'inbox', component: InboxMessageComponent },
            { path: '', component: InboxMessageComponent },
            { path: '**', component: NotFoundComponent }
        ])
    ],
    exports: [RouterModule]
})

收件箱-routing.module.ts

@NgModule({
    imports: [
        RouterModule.forChild([
            {path: '/inbox/list',component: InboxMessageListComponent},
            {path: '/inbox/detail/:id',component: InboxMessageDetailComponent}
        ])
    ],
    exports: [RouterModule]
})

app.component.ts

template : '<router-outlet></router-outlet>'<!--This route-outlet will load "LoginComponent/InboxComponent" depending upon the routing. -->

收件箱-message.component.ts

template:`
<sidebar-component></sidebar-component>
<router-outlet></router-outlet> <!-- This will load InboxMessageListComponent or InboxMessageDetailComponent -->
`

但问题是一次只有一个工作。第二个是跳过。

如何处理此类项目?

2 个答案:

答案 0 :(得分:5)

您必须声明一条主路径和childer路径,例如:

import {NgModule}             from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {AddProject} from './add-project.component';
import {ViewProject} from './view-project.component';
import {Project} from './charity-project.component';
import {ProjectList} from './charity-project-list.component';

export const routes: Routes = [
    {
        path: 'project', component: Project,
        children: [
            { path: '', component: ProjectList },
            { path: 'add', component: AddProject },
            { path: 'view/:id', component: ViewProject },
            { path: 'edit/:id', component: AddProject }
        ]
    }
];

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

export class ProjectRoutes { }

在儿童路径中,您必须声明

3) path:/inbox/all , "Load InboxMessageListComponent in Left side Box Only",
4) path:/inbox/13 , "Load InboxMessageDetailComponent in Right side Box Only"

答案 1 :(得分:0)

我认为inboxapp应用程序的模块。在这种情况下,您还应该导入模块inbox中的app模块,如下所示:

@NgModule({
    imports: [
        RouterModule.forRoot([
            { path: 'login', component: LoginComponent},
            { path: 'inbox', component: InboxMessageComponent },
            { path: '', component: InboxMessageComponent },
            { path: '**', component: NotFoundComponent }
        ]),
        MyInboxModule
    ],
    exports: [RouterModule]
})

另请参阅Angular2中的路由和Natigation链接:https://angular.io/docs/ts/latest/guide/router.html

希望这有帮助。