【问题标题】:Angular 9 Unhandled Promise rejection: Angular JIT compilation failed errorAngular 9 Unhandled Promise 拒绝:Angular JIT 编译失败错误
【发布时间】:2020-04-21 08:38:54
【问题描述】:

我将我们的应用程序升级到 Angular 9,但在生产测试中我收到了这个错误。在将其部署到生产环境之前,我正在使用命令 ng serve --optimization=true --aot=true --prod 为我的应用程序提供服务,但它只是无法正常工作

Unhandled Promise rejection: Angular JIT compilation failed: '@angular/compiler' not loaded!
  - JIT compilation is discouraged for production use-cases! Consider AOT mode instead.
  - Did you bootstrap using '@angular/platform-browser-dynamic' or '@angular/platform-server'?
  - Alternatively provide the compiler with 'import "@angular/compiler";' before bootstrapping. ; Zone: <root> ;

这是指向我的 polyfills.ts

我已将 import "@angular/compiler 添加到我的 main.ts 文件的顶部,但没有成功。

我还从 polyfill 中删除了除 zone.js 之外的所有导入,但仍然出现错误。

这是什么意思,如何解决?

【问题讨论】:

  • 尝试使用ng build --prod 编译生产环境,并使用lite-server 之类的东西作为per docs 提供服务。
  • 编译不是问题,它运行出现错误的应用程序。我将它部署到了一个模拟我们的生产环境的测试服务器上,并且出现了同样的错误

标签: angular


【解决方案1】:

您需要在 main.ts 文件的最顶部导入“@angular/compiler”,但既然您已经拥有了,那么

你必须在你的 package.json 中运行它

scripts{
"postinstall": "ngcc --properties es5 browser module main --first-only"
}

【讨论】:

  • 它采用非 Ivy 库并生成 Ivy 可以理解的文件。如果没有给出属性,ngcc 将处理所有的包格式(fesm2015、fesm5、es2015、esm2015、esm5、main、module),这可能不是我们想要的。 Flag first-only 告诉 ngcc 处理它在 package.json 中找到的第一个属性,否则它将处理上面列出的所有条目。并且 create-ivy-entry-points 将告诉 ngcc 为 Ivy 生成的条目创建新属性,而不是覆盖前一个
猜你喜欢
  • 2018-02-25
  • 2021-05-25
  • 2020-05-27
  • 1970-01-01
  • 2021-11-04
  • 2017-03-01
  • 2022-01-02
  • 1970-01-01
  • 2020-05-28
相关资源
最近更新 更多