angular 手动编译_如何编译运行时生成的Angular8代码?

I'm creating Angular code at runtime, in particular, I use a SVG library in order to create a vector graphic that contains Angular code directives like (click)='myMethod()', which, in turn, call methods I've statically defined in the SVG-enclosing component. Being generated at runtime I need to compile the created template and add it to a component. I implemented such code back then with Angular 3 with the help of this post which was quite cumbersome. I tried to the copy the old code in an Angular 8 app:

private addComponent(template: string) {

@Component({template: template + '

class TemplateComponent {

@ViewChild('target', {static: false, read: ViewContainerRef}) public target;

constructor() {

}

public myMethod() {

// do something

}

}

@NgModule({declarations: [TemplateComponent]})

class TemplateModule {

@ViewChild('target', {static: false, read: ViewContainerRef}) public target;

}

// ERROR in next line:

const mod = this.compiler.compileModuleAndAllComponentsSync(TemplateModule);

const factory = mod.componentFactories.find((comp) =>

comp.componentType === TemplateComponent

);

this.container.createComponent(factory);

}

which now fails with

ERROR Error: Runtime compiler is not loaded

at Compiler._throwError (core.js:38932)

On the one hand, I have no clue why that error occurs. Everything I found on the internet was about critical function syntax in lazy module loading. On the other hand, I wonder, if five Angular major versions later, there is an other way to do it. I read about Portals but it seems to be about loading static templates dynamically, but not uncompiled templates generated on the fly. Looking forward to someone pointing me in the right direction.

Post Scriptum: Adding a Bounty for the one that can provide a basic running code snippet targetting Angular v9 in AoT mode. It has to contain a runtime-compiled template (e.g. from a string variable) containing a method call in the associated component.

解决方案

The JIT compiler is now excluded by default from AOT bundles so you have to include it manually. You need to install the package @angular/platform-browser-dynamic and add the Compiler providers to your application module. For example:

import { NgModule, COMPILER_OPTIONS, CompilerFactory, Compiler } from '@angular/core';

import { JitCompilerFactory } from '@angular/platform-browser-dynamic';

@NgModule({

providers: [

{ provide: COMPILER_OPTIONS, useValue: {}, multi: true },

{ provide: CompilerFactory, useClass: JitCompilerFactory, deps: [COMPILER_OPTIONS] },

{ provide: Compiler, useFactory: createCompiler, deps: [CompilerFactory] }

]

})

export class AppModule { }

export function createCompiler(compilerFactory: CompilerFactory) {

return compilerFactory.createCompiler();

}

Then you have to also make some small changes to your code because you cannot set the template to be a variable using the @Component decorator it gives a compile error. The decorator must be run dynamically in code.

Here is your method updated with the changes:

private addComponent(template: string) {

class TemplateComponent {

@ViewChild('target', {static: false, read: ViewContainerRef}) public target;

constructor() {

}

public myMethod() {

// do something

}

}

class TemplateModule {

@ViewChild('target', {static: false, read: ViewContainerRef}) public target;

}

const componentType = Component({template: template + '

const componentModuleType = NgModule({declarations: [componentType]})(TemplateModule)

const mod = this.compiler.compileModuleAndAllComponentsSync(componentModuleType);

const factory = mod.componentFactories.find((comp) =>

comp.componentType === componentType

);

this.container.createComponent(factory);

}

I have created also a StackBlitz sample where you can see it working.

For a more detailed sample on how to do this check this Angular AOT Dynamic Components GitHub repository with a full sample.

I found this in the Angular GitHub issue about dynamically loading component templates. Since you use this it is probably good you follow the issue to be up to date on the developments.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值