我在通过API响应“即时”构建模板时遇到了问题,但仅在AoT构建中存在问题。
我从后端收到了这样的回复:
<h1>Title...</h1>
<some-component></some-componen>
<p>other content</p>
Run Code Online (Sandbox Code Playgroud)
我想像常规的Angular模板一样解析它。
我组件的简化代码如下所示:
import {
Compiler,
Component,
ComponentFactory,
ComponentRef,
Injector,
Input,
NgModule,
OnChanges,
OnDestroy,
OnInit,
ViewContainerRef
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
export async function createComponentFactory(compiler: Compiler, metadata: Component): Promise> {
const cmpClass = class DynamicComponent {
};
const decoratedCmp = Component(metadata)(cmpClass);
// IMPORT ALL MODULES HERE!!!
@NgModule({imports: [CommonModule, RouterModule], declarations: [decoratedCmp]})
class DynamicHtmlModule {
}
const moduleWithComponentFactory = await compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule);
return moduleWithComponentFactory.componentFactories.find(x => x.componentType === …Run Code Online (Sandbox Code Playgroud)