angular - 在 Angular 延迟加载中找不到模块

标签 angular angular-routing angular-cli-v7

我在Mac环境下成功完成了一个angular项目

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

现在我使用设置在 ubuntu 18.04 上运行相同的代码

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

但是在尝试延迟加载另一个模块时出现了一个非常奇怪的问题,我不断收到此错误错误:找不到模块“app/website/site.module”

这是我的项目结构

enter image description here

和 app-routing.module.ts

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

路由在mac上用过,在ubuntu上失败

我查找了不同的解决方案

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

但还是不行。

最佳答案

我在 stackblitz 上创建了一个示例项目

解释:

所以首先,你必须像这样创建你的路线:

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

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

然后将其添加到应用模块(根模块):

@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 { }

然后您必须将路由添加到子模块(在本例中为 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 { }

现在它应该可以工作了,如果你有任何问题我会在这里:)

关于angular - 在 Angular 延迟加载中找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57764623/

相关文章:

angular - ng-select 在 Angular 6 中以编程方式删除元素

javascript - 正则表达式设置单元测试文件路径

javascript - 使用延迟加载时如何在 Angular 中动态构建导航?

javascript - 未捕获错误 : Syntax error, 无法识别的表达式:/contact - ANGULAR 2 - 路由时无法实现平滑滚动效果

node.js - Angular CLI 项目无法编译

css - 如何为 Ionic 中的每个事件选项卡设置不同的颜色

Angular2 - APP_BASE_HREF 与 HashLocationStrategy

angular - 如何在发送之前动态更改 proxy.conf.js 中的请求

angular - ng upgrade 命令无法将 cli 从 v7 升级到 v8

javascript - 更改范围 slider 的颜色