ERROR Error: Uncaught (in promise): Error: Cannot find module './user/user.module'

ERROR Error: Uncaught (in promise): Error: Cannot find module './user/user.module'

我正在尝试延迟加载模块并遇到以下错误

ERROR Error: Uncaught (in promise): Error: Cannot find module './user/user.module'

用户模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { userRoutes } from './user.routes';
import { ProfileComponent } from './profile.component';

@NgModule({
  imports: [CommonModule, RouterModule.forChild(userRoutes)],
  declarations: [ProfileComponent],
  providers: []
})
export class UserModule {}

在 userRoutes
{ path: 'user', loadChildren: './user/user.module#UserModule' }

中添加了我的路径

错误堆栈已满

core.js:6241 ERROR Error: Uncaught (in promise): Error: Cannot find module './user/user.module'
Error: Cannot find module './user/user.module'
    at $_lazy_route_resource lazy namespace object:5
    at ZoneDelegate.invoke (zone-evergreen.js:364)
    at Object.onInvoke (core.js:41667)
    at ZoneDelegate.invoke (zone-evergreen.js:363)
    at Zone.run (zone-evergreen.js:123)
    at zone-evergreen.js:857
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Object.onInvokeTask (core.js:41645)
    at ZoneDelegate.invokeTask (zone-evergreen.js:398)
    at Zone.runTask (zone-evergreen.js:167)
    at resolvePromise (zone-evergreen.js:798)
    at resolvePromise (zone-evergreen.js:750)
    at zone-evergreen.js:860
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Object.onInvokeTask (core.js:41645)
    at ZoneDelegate.invokeTask (zone-evergreen.js:398)
    at Zone.runTask (zone-evergreen.js:167)
    at drainMicroTaskQueue (zone-evergreen.js:569)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:484)
    at invokeTask (zone-evergreen.js:1621)

加载模块的新方法是使用箭头函数

将以下路径添加到应用程序路由 ts

{ path: 'user', loadChildren: ()=> import('./user/user.module').then(m => m.UserModule)}:

然后在你的延迟加载模块用户路由中添加一个这样的

const routes: Routes = [
  {
    path: '',
    component: UsersComponent
  }
];

如果您在最新版本中创建 angular 应用程序,请使用上述方法。

您是否尝试仅在 UserModule 内部使用 UserModule? 您提到您正在 userRoutes 中定义延迟加载部分。那是不正确的。 请在app-routing.module.ts中定义 路径应该是相对于 app-routing.module.ts