Angular 报错 'ERROR Error: Uncaught (in promise): Error: The NgModule 'MainModule' needs to be compiled using the JIT compiler, but '@angular/compiler' is not available.' 解决方法
这个错误通常是因为在使用 AOT 编译时,没有正确配置应用程序以使用 JIT 编译器。
要解决这个问题,可以尝试以下几个步骤:
-
确保在应用程序的 'tsconfig.json' 文件中,'angularCompilerOptions' 的 'AOT' 属性为 'true'。
-
确保在应用程序的 'main.ts' 文件中,'platformBrowserDynamic().bootstrapModule()' 方法使用了 'JitCompilerFactory'。
例如:
import { JitCompilerFactory } from '@angular/compiler';
...
const compilerFactory = new JitCompilerFactory();
const compiler = compilerFactory.createCompiler();
...
platformBrowserDynamic().bootstrapModule(AppModule, {
compiler: compiler
})
- 确保在应用程序的 'app.module.ts' 文件中,导入了 'BrowserAnimationsModule' 和 'NoopAnimationsModule'。
例如:
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
...
@NgModule({
imports: [
...
BrowserAnimationsModule,
NoopAnimationsModule,
...
],
...
})
export class AppModule { }
如果以上步骤都无法解决问题,可以尝试重新安装 '@angular/compiler' 依赖项,或者升级到最新版本。
原文地址: https://www.cveoy.top/t/topic/oCJq 著作权归作者所有。请勿转载和采集!