Angular: 如何导入JitCompilerFactory?

Angular: How to import JitCompilerFactory?

如果我用AOT编译动态编译就不行了。所以我需要将编译器加载到浏览器。那么我该如何加载呢?如果我使用

import { JitCompilerFactory } from '@angular/compiler';

但是,在导入 JitCompilerFactory 之后,我收到以下错误:

"export 'JitCompilerFactory' was not found in 'angular/compiler

所以我是对的,现在我需要从 'angular/platform-browser-dynamic' 导入它以进行动态编译?

您需要像这样将 JitCompilerFactory 导入到您的 app.module.ts 中:

import {Compiler, COMPILER_OPTIONS, CompilerFactory} from '@angular/core';
import {JitCompilerFactory} from '@angular/platform-browser-dynamic';
export function createCompiler(compilerFactory: CompilerFactory) {
  return compilerFactory.createCompiler();
}



@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule
  ],
  providers: [
    {provide: COMPILER_OPTIONS, useValue: {}, multi: true},
    {provide: CompilerFactory, useClass: JitCompilerFactory, deps: [COMPILER_OPTIONS]},
    {provide: Compiler, useFactory: createCompiler, deps: [CompilerFactory]}
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

我在这里创建了一个完全可用的 StackBlitz Demo 动态组件,如果你想在那里玩的话。