1

If I use AOT compilation dynamic compilation will not possible. So I need to load the compiler to a browser. So how do I load it? If I use

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

However, after importing the JitCompilerFactory I am getting the following error:

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

So am I right that now I need to import it from 'angular/platform-browser-dynamic' for dynamic compilation?

Narm
  • 10,677
  • 5
  • 41
  • 54

1 Answers1

6

You need to import JitCompilerFactory into your app.module.ts like this:

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 {}

Here I created a fully working StackBlitz Demo with a Dynamic Component if you want to play around with it on there.

Narm
  • 10,677
  • 5
  • 41
  • 54
  • @angularindepth-com yes, it helped. – Dmitry Anikeychik Aug 01 '18 at 09:19
  • Thanks for this example. One thing to note is that `core-js` has changed its import paths in `3.0.1`. I had to add this to my polyfills.ts: `import 'core-js/es/reflect'; import 'core-js/stable/reflect'; import 'core-js/features/reflect';`. – k0nG Apr 16 '19 at 10:18
  • @Narm I'm trying this example, but getting an error: ``` ERROR Error: No NgModule metadata found for 'DynamicComponentModule'. ``` I think I'm doing it exactly the same way as you're doing it, plus I updated the dependencies on your example like they are on my project and it's still working for you, but not for me. Any idea on what else I should take care of? – gencblakqori Jul 29 '19 at 14:13
  • @Narm Actually, when I run ng serve --aot=true on your project, then I'm getting the same error. – gencblakqori Jul 29 '19 at 15:05
  • Hi @gencblakqori, the comment section to this question isn't the correct forum to answer these questions. If you create your own question with your unique problem I can take a look at it and we can discuss it there. – Narm Jul 29 '19 at 16:27
  • 4
    Hi @Narm, it's the same topic. You're also doing dynamic compilation. In fact I just downloaded your code from StackBlitz and ran `ng serve --aot` and it didn't work. It showed me the error above. – gencblakqori Jul 30 '19 at 08:05