如果我使用AOT编译,动态编译是不可能的。因此,我需要将编译器加载到浏览器中。那么我如何加载它呢?如果我使用
import { JitCompilerFactory } from '@angular/compiler';
然而,在导入JitCompilerFactory
之后,我得到了以下错误:
“在‘angular/编译器’中未找到导出'JitCompilerFactory‘
那么,我现在需要从'angular/platform-browser-dynamic'
导入它以进行动态编译,这对吗?
发布于 2018-07-30 00:49:18
您需要将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,如果你想在上面使用它的话。
https://stackoverflow.com/questions/51582488
复制