首页 > 解决方案 > **build --prod** 时延迟加载不起作用

问题描述

> app-routing.module.ts

const routes: Routes = [
  { path: '', redirectTo: 'test', pathMatch: 'full' },
  {
    path: 'test',
    loadChildren: () => import('./test-lazy-loading/test-lazy-loading.module').then(m => m.TestLazyLoadingModule)
  }
];
ERROR
GET http://localhost/styles.09e2c710755c8867a460.css net::ERR_ABORTED 404 (Not Found)
2localhost/:12 GET http://localhost/runtime-es2015.eb2b81c6532ef7a6ad37.js net::ERR_ABORTED 404 (Not Found)

它在运行ng serve时工作,但在构建ng build --prod时它不工作。请帮我!谢谢

标签: angularlazy-loading

解决方案


我到处寻找,但没有找到比禁用aot(Ahead Of Time)编译更好的解决方案。跑步ng build --prod --aot=false --build-optimizer=false可以解决问题。您也可以禁用aotangular.json

{
   "projects":{
      "<your-project-name>":{
         "architect":{
            "build":{
               "configurations":{
                  "configurations":{
                     "production":{
                        "aot":false
                     }
                  }
               }
            }
         }
      }
   }
}

aot 和 jit(及时)之间的区别在于模块是在 jit 中动态编译的,就像在开发模式下使用ng serve. 这会加快编译速度,但可能会影响您在生产中的性能。

当时aot似乎不支持aot或loadchildren。


推荐阅读