Angular loadChildren 没有做任何事情

Angular loadChildren isn't doing anything

我正在学习 PluralSight 上的 Angular 基础知识 课程,创建一个带有用户模块和事件模块的事件管理站点。 (该课程没有将事件管理放入功能模块,但我自己做了。)我按照说明让每个模块处理自己的路由,使用 loadChildren顶级路由:

app.routes.ts

import { Routes } from '@angular/router';
import { NotFoundComponent } from './utility/not-found.component';

export const appRoutes: Routes = [
  { 
      path: 'events', 
      loadChildren: './events/events.module#EventsModule'
  },
  { path: 'user', loadChildren: './user/user.module#UserModule'},
  { path: 'NotFound', component: NotFoundComponent },
  { path: '', redirectTo: '/events', pathMatch: 'full' },
  { path: '**', component: NotFoundComponent }
];

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

import { appRoutes } from './app.routes';

import { AppComponent } from './app.component';
import { SharedModule } from './common/shared.module';
import { NavBarComponent } from './nav/navbar.component';
import { EventService } from './events/event.service';
import { ToastrService } from './common/toastr.service';
import { NotFoundComponent } from './utility/not-found.component';
import { UserModule } from './user/user.module';
import { EventsModule } from './events/events.module';


@NgModule({
  declarations: [
    AppComponent,
    NavBarComponent,
    NotFoundComponent
  ],
  imports: [
    BrowserModule,
    SharedModule,
    EventsModule,
    UserModule,
    RouterModule.forRoot(appRoutes)
  ],
  providers: [
    EventService,
    ToastrService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后,据推测,如果我要/events显示事件列表,/events/create显示页面用于输入新事件,以及 /events/3 以显示 id = 3 事件的详细信息,我应该具有以下内容。根据课程,由 Angular's own exposition on the subject 证实,我应该只指定子路径,省略据称已经由应用程序级路由考虑的 "events" 段。

来自event.routes.ts

export const eventRoutes: Routes = [
    { path: 'create', component: CreateEventComponent, canDeactivate: [UnsavedNewEventGuard] },
    { path: ':id', component: EventDetailsComponent, canActivate: [ValidEventGuard] },
    { path: '', component: EventsListComponent }
];

events.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { eventRoutes } from './event.routes';
import { EventsListComponent } from './event-list/events-list.component';
import { CreateEventComponent } from './create-event/create-event.component';
import { EventThumbnailComponent } from './event-list/event-thumbnail.component';
import { EventDetailsComponent } from './event-details/event-details.component';

@NgModule({
  declarations: [
    EventsListComponent,
    CreateEventComponent,
    EventThumbnailComponent,
    EventDetailsComponent,
  ],
  imports: [
    CommonModule,
    RouterModule.forChild(eventRoutes)
  ]
})
export class EventsModule { }

这行不通。相反,它直接显示路径 / 的事件列表(即 https://localhost:4200/,它不会重定向)。它不会路由到 /events、/events/create 或 /events/3.

当我提供完整路径,包括我认为不需要的 "events" 部分时,然后该应用程序运行:

event.routes.ts(版本 2)

export const eventRoutes: Routes = [
    { path: 'events/create', component: CreateEventComponent, canDeactivate: [UnsavedNewEventGuard] },
    { path: 'events/:id', component: EventDetailsComponent, canActivate: [ValidEventGuard] },
    { path: 'events', component: EventsListComponent }
];

或者,将子路径嵌套到 子路径 属性 的父路径下 "events" 也可以:

event.routes.ts(版本 3)

export const eventRoutes: Routes = [{
  path: 'events', children: [
    { path: 'create', component: CreateEventComponent, canDeactivate: [UnsavedNewEventGuard] },
    { path: ':id', component: EventDetailsComponent, canActivate: [ValidEventGuard] },
    { path: '', component: EventsListComponent }
  ]
}];

但是版本 2 和版本 3 工作 无论我的应用程序级路由文件 中是否有带有 loadChildren 属性的路由。应用对此很满意:

app.routes.ts(版本 2)

import { Routes } from '@angular/router';
import { NotFoundComponent } from './utility/not-found.component';

export const appRoutes: Routes = [
  { path: 'NotFound', component: NotFoundComponent },
  { path: '', redirectTo: '/events', pathMatch: 'full' },
  { path: '**', component: NotFoundComponent }
];

基本上,loadChildren 路由被忽略了。 (同样适用于用户模块。)我不明白为什么。

您正在 app.module.ts 上导入模块。您的 "version 2 and 3" 工作的原因是模块被导入到您的 AppModule 中。要使延迟加载正常工作,您需要从 AppModule:

中删除 EventsModuleUserModule
@NgModule({
  declarations: [...],
  imports: [
    BrowserModule,
    SharedModule,
    RouterModule.forRoot(appRoutes)
  ],
  providers: [...],
  bootstrap: [AppComponent]
})
export class AppModule { }

此外,您可能需要 re-run ng serve 添加新模块以进行延迟加载。