代码之家  ›  专栏  ›  技术社区  ›  POV

为什么不为组件工作子路由?

  •  0
  • POV  · 技术社区  · 5 年前

    文件 AppRoutingModule 包含:

    const routes: Routes = [
    {
        path: "events",
        component: EventComponent,
        children: [
          {
            path: 'add',
            component: AddEventComponent
          }
        ]
      },
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    

    我试图达到 AddEventComponent 通过URL: localhost:4200/events/add

    它没有打开,没有错误。

    成分 添加事件组件 注册于 add-event.module.ts :

    import { NgModule } from "@angular/core";
    import { SharedModule } from 'src/app/SharedModule';
    import { CompanyService } from 'src/app/api/CompanyService';
    import { AddEventComponent } from './add-event-component';
    
    @NgModule({
      imports: [SharedModule],
      declarations: [AddEventComponent],
      exports: [AddEventComponent],
      providers: [CompanyService]
    })
    export class AddEventModule { }
    

    然后 AddEventModule 已添加到主 AppModule :

    @NgModule({
      declarations: [
      ],
      imports: [
         AddEventModule
    ]});
    

    所以,我需要打开组件 添加事件组件 通过URL: 本地主机:4200/events/add ,现在它总是打开父级 EventComponent

    附加模块 :

    @NgModule({
      declarations: [
        AppComponent,
        NofoundComponent
      ],
      imports: [
        CommonModule,
        BrowserModule,
        HttpClientModule,
        SharedModule,
        LoginModule,
        LanguageModule,
        EventModule,
        AddEventModule,
        VisitorModule,
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: CustomTranslateLoader,
            deps: [HttpClient]
          }
        }),
    
        SocketIoModule.forRoot(config),
        AppRoutingModule
      ],
      providers: [{
        provide: HTTP_INTERCEPTORS,
        useClass: RequestInterception,
        multi: true,
      },
        EventsService, VisitorsService, UserService, ExportService, AuthenticationGuard],
      exports: [TranslateModule, AddEventModule],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Alexander Staroselsky    5 年前

    EventComponent <router-outlet></router-outlet> child routing children

    <!-- existing component content -->
    <router-outlet></router-outlet>
    

    <

    <

    const routes: Routes = [
      { path: "events", component: EventComponent },
      { path: "events/add", component: AddEventComponent }
    ];
    

    AddEventComponent