gpt4 book ai didi

angular - 如何在 Angular 2 中路由多个组件

转载 作者:太空狗 更新时间:2023-10-29 17:05:27 27 4
gpt4 key购买 nike

我有一个 Angular 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。

app-routing.module.ts

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

inbox-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. -->

inbox-message.component.ts

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

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

这种项目如何路由?

最佳答案

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

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"

关于angular - 如何在 Angular 2 中路由多个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40397056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com