代码之家  ›  专栏  ›  技术社区  ›  Blake Rivell

组件正在显示,但路由器出口在角度上被忽略

  •  -1
  • Blake Rivell  · 技术社区  · 6 年前

    http://localhost:4200/admin/users

    文件的结构如下:

    /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)
              ...
    

    我有三个级别的路线:

        // 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}
        ]
      }
    ];
    

    @NgModule({
      declarations: [
        ManageUsersComponent,
        UserListComponent,
        UserDetailComponent
      ],
      imports: [
        CommonModule,
        SharedModule,
        ManageUsersRoutingModule
      ],
      providers: [
        UserService
      ]
    })
    export class ManageUsersModule { }
    

    下面是我的Admin.module的外观:

    @NgModule({
      declarations: [
        AdminComponent
      ],
      imports: [
        CommonModule,
        AdminRoutingModule,
        ManageUsersModule
      ]
    })
    export class AdminModule { }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Nico    6 年前

    你需要使用 children 属性,以便加载路由器出口内的组件。

    您设置路由的方式是不正确的,您使用的是用户路由的路由模块,但是您将该模块导入UserModule之外,实际上要做的是追加到管理模块内部的现有路由。

    所以这才是真正发生的事情:

    const adminRoutes: Routes = [
      {
        path: '',
        component: AdminComponent,
        children: [
          {
            path: '',
            children:
            [
               { path: 'users', component: ManageUsersComponent }
            ]
          }
        ]
      },
        ...manageTenantRoutes   // your routes are being added here
    ];
    

    解决这个问题的一个方法是像使用管理模块那样使用延迟加载。

    如果您想保持这种方式,那么您必须准确地告诉这些路由应该附加到哪里。

    更新见stackblitz

    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}
        ]
      }
    ];
    

    希望这能解决问题!