我在app.component.html,admin.component.html和manage-users.component.html中有一个路由器插座。出于某种原因,当我导航到http://localhost:4200/admin/users
时,manage-users.component.html中的路由器出口未输出任何内容文件的结构如下:
/Admin (Module)
admin.component.html (has router-outlet)
admin.component.ts
admin.module.ts
admin-routing.module.ts
/Manage-Users (Module within Admin module)
manage-users.component.html (has router-outlet)
manage-users.component.ts
manage-users.module.ts
manage-users-routing.module.ts
/User-List (just a component to be displayed in manage-users outlet)
...
/User-Detail (just a component to be displayed in manage-users outlet)
...
我有3种路线:
// App Routes (app-routing.module.ts)
const appRoutes: Routes = [
{ path: 'admin', loadChildren: './admin/admin.module#AdminModule', canLoad: [AuthGuard] },
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
// Admin Module Routes (admin-routing.module.ts)
const adminRoutes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{ path: 'users', component: ManageUsersComponent }
]
}
];
// Admin Module > Manage Users Module Routes (manage-users-routing.module.ts)
const manageUsersRoutes: Routes = [
{
path: '',
component: ManageUsersComponent,
children: [
{path: '', component: UserListComponent},
{path: 'edit/:id', component: UserDetailComponent}
]
}
];
下面是我的ManageUsers.module的样子:
@NgModule({
declarations: [
ManageUsersComponent,
UserListComponent,
UserDetailComponent
],
imports: [
CommonModule,
SharedModule,
ManageUsersRoutingModule
],
providers: [
UserService
]
})
export class ManageUsersModule { }
下面是我的管理模块的样子:
@NgModule({
declarations: [
AdminComponent
],
imports: [
CommonModule,
AdminRoutingModule,
ManageUsersModule
]
})
export class AdminModule { }
答案 0 :(得分:1)
您需要在用户路由内使用children
属性,以便在该路由器出口内加载组件。
设置路由的方式不正确您正在为用户路由使用路由模块,但是您将该模块导入UserModule之外,实际上是将其添加到管理模块内部的现有路由中。
这就是实际情况:
const adminRoutes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{
path: '',
children:
[
{ path: 'users', component: ManageUsersComponent }
]
}
]
},
...manageTenantRoutes // your routes are being added here
];
解决此问题的一种方法是像管理模块一样使用延迟加载。
如果要保持这种方式,则必须准确告知这些路由的附加位置。
有关更新,请参见堆栈闪电战
https://stackblitz.com/edit/angular-ryjdxc
但是您必须像这样添加它:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AdminComponent } from './admin.component';
import { ManageUsersComponent } from './manage-users/manage-users.component';
import { manageTenantRoutes } from './manage-users/manage-users-routing.module'
const adminRoutes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{
path: '',
children:
[
...manageTenantRoutes
]
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(adminRoutes)],
exports: [RouterModule]
})
export class AdminRoutingModule { }
并更改您的路线以反映此更改
export const manageTenantRoutes: Routes = [
{
path: 'users',
component: ManageUsersComponent,
children: [
{path: '', component: UserListComponent},
{path: 'edit/:id', component: UserDetailComponent}
]
}
];
希望这可以清除它!