简体   繁体   English

在角度延迟加载中找不到模块

[英]cannot find module in angular Lazy Loading

I have an angular project successfully on the Mac environment我在 Mac 环境下成功创建了一个 angular 项目

Angular CLI: 7.0.5
Node: 8.11.3
OS: darwin x64
Angular: 7.0.3

Now I am running the same code on ubuntu 18.04 with the setup现在我在 ubuntu 18.04 上使用设置运行相同的代码

Angular CLI: 7.3.9
Node: 12.9.1
OS: linux x64
Angular: 7.2.15

however it is coming with a very weird issue when trying to lazy load another module, I keep getting this error Error: Cannot find module "app/website/site.module"但是在尝试延迟加载另一个模块时出现了一个非常奇怪的问题,我不断收到此错误错误:找不到模块“app/website/site.module”

Here is my project structure这是我的项目结构

在此处输入图片说明

and app-routing.module.ts和 app-routing.module.ts

 const routes: Routes = [    
      {
        path: 'site',
        loadChildren: 'app/website/site.module#SiteModule',
      }
    ]

the routing is used to work in mac but failed in ubuntu路由用于在mac中工作但在ubuntu中失败

I looked up a different solution我查了一个不同的解决方案

const rootRoutes: Routes = [
  { path: 'site', loadChildren: './website/site.module#SiteModule' }
];

but still it failed to work.但它仍然无法正常工作。

I created an example project on stackblitz我在stackblitz上创建了一个示例项目

Explanation:解释:

So first of all, you have to create your routes like this:所以首先,你必须像这样创建你的路线:

const routes: Routes = [
  // this will get lazy loaded
  { 
    path: 'lazyload',
    loadChildren: () => import('./module/module.module').then(mod => mod.ModuleModule) 
  },

  // default route
  { 
    path: '**',
    component: Component1Component
  }
];

Then add it to app module (root module):然后将其添加到 app 模块(根模块):

@NgModule({
  imports:      [
    // your imports here ...
    // add your routes (forRoot() is only in the root module!)
    RouterModule.forRoot(routes)
  ],
  // your providers, declarations, etc.
})
export class AppModule { }

Then you have to add routes to the child module (in this case ModuleModule):然后你必须向子模块添加路由(在本例中为 ModuleModule):

const routes: Routes = [
  { path: 'route1', component: Component2Component },
  { path: '', component: Component3Component }
];

@NgModule({
  imports: [
    // your imports here ... 
    RouterModule.forChild(routes)
  ],
  declarations: [Component2Component, Component3Component]
})
export class ModuleModule { }

now it should work, if you have any problems I will be here :)现在它应该可以工作了,如果您有任何问题,我会在这里:)

I had a similar problem, in my code it was the same path like your我遇到了类似的问题,在我的代码中,它与您的路径相同

  {
      path: 'admin', loadChildren: './admin/admin.module#AdminModule'
  }

and after I changed file tsconfig.app.json在我更改文件 tsconfig.app.json 之后

it began to work and find child route, on attached image you can see changes that i done它开始工作并找到子路径,在附加的图像上你可以看到我所做的更改

在此处输入图片说明

In my particular case, Pavel B. solution worked best.在我的特殊情况下,Pavel B. 解决方案效果最好。 No warnings at compilation or runtime!在编译或运行时没有警告!

  {
    path: 'users',
    loadChildren: () => import('./users/users.module').then(mod => mod.UsersModule)
  }

Using:使用:

Angular CLI: 9.1.7 Node: 12.14.0 OS: darwin x64 Angular CLI:9.1.7 节点:12.14.0 操作系统:darwin x64

Angular: 9.1.9 ... animations, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, router Ivy Workspace: Yes Angular:9.1.9 ...动画、通用、编译器、编译器cli、核心、表单...平台浏览器、平台浏览器动态、路由器常春藤工作区:是的

Package and Versions包和版本

@angular-devkit/architect         0.901.7
@angular-devkit/build-angular     0.901.7
@angular-devkit/build-optimizer   0.901.7
@angular-devkit/build-webpack     0.901.7
@angular-devkit/core              9.1.7
@angular-devkit/schematics        9.1.7
@angular/cli                      9.1.7
@ngtools/webpack                  9.1.7
@schematics/angular               9.1.7
@schematics/update                0.901.7
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0

你可以这样使用:

const rootRoutes: Routes = [ { path: 'site', loadChildren: () => SiteModule } ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM