升级到Ionic 5报错Error: Angular JIT compilation failed: '@angular/compiler' not loaded!

时间:2023-05-12 17:42:43

一个app使用的是Ionic以及angular开发的,升级到Ionic 5,angular 9。升级如下:

npm install @ionic/angular@latest @ionic/angular-toolkit@latest --save-exact --save
ng update @angular/core @angular/cli

运行时报错:

Error: 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.
at getCompilerFacade (core.js:610)
at Function.get (core.js:16065)
at getInjectableDef (core.js:362)
at injectableDefOrInjectorDefFactory (core.js:16816)

解决方法一(推荐)

在main.ts文件里导入@angular/compiler。

import '@angular/compiler';

方法二:

在angular.json,把"aot": true 改为 "aot: false。

方法三:

在ionic执行命令添加--aot=false:

ionic serve -- --aot=false

其中方法二和方法三是不推荐,aot会提高性能。