我实际上找到了一个适合我们的解决方案,唯一的缺点是我们必须为该项目禁用 AoT 编译。
对于那些在我之后想要同样事情的人 - 这是我最终做了什么的快速概述。
首先,在这里找到我的解决方案的基础:https://stackblitz.com/edit/mlc-app-init-zyns9l?file=app%2Fapp.component.ts,我将其放入共享助手中:
import { Component, NgModule, Compiler, ViewContainerRef, Type } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { SharedModule } from '../modules/shared.module';
export class DynamicComponentHelper {
public static addComponent<ViewModelType = any>(compiler: Compiler, container: ViewContainerRef, template: string, viewModel?: ViewModelType, components: Array<Type<any>> = []): void {
@Component({ template: template })
class DynamicComponent {
public vm: ViewModelType = viewModel;
constructor() { }
}
components.push(DynamicComponent);
@NgModule({
imports: [BrowserModule, SharedModule],
declarations: components
})
class DynamicComponentModule { }
const mod = compiler.compileModuleAndAllComponentsSync(DynamicComponentModule);
const factory = mod.componentFactories.find((comp) =>
comp.componentType === DynamicComponent
);
const component = container.createComponent(factory);
}
}
然后我有一个组件这样调用它......
export interface VM { text: string; }
@Component({
selector: 'app-component',
template: `<ng-template #dynamicComponent></ng-template>`
...
})
export class VariationsComponent implements OnInit, AfterViewInit {
@ViewChild('dynamicComponent', { read: ViewContainerRef }) _container: ViewContainerRef;
private vm: VariationsComponentVM = { text: 'Hello World' }
private viewInitialized: boolean = false;
private componentTemplate: string;
constructor(private compiler: Compiler) { }
ngAfterViewInit(): void {
this.viewInitialized = true;
this.setUpDynamicComponent();
}
ngOnInit(): void {
this.httpService.getComponentTemplate().subscribe(template => {
this.componentTemplate = template;
this.setUpDynamicComponent();
});
}
private setUpDynamicComponent(): void {
if (this.viewInitialized === true && this.componentTemplate) {
DynamicComponentHelper.addComponent(this.compiler, this._container, this.componentTemplate, this.vm, [NestedComponent]);
}
}
}
然后实际使用的模板可能很简单......
<div>{{ vm.text }}</div>
<app-nested [input]="vm.text"></app-nested>
关于这个的重要部分是所有内置的角度模板工作,所有指令和一切。再一次,唯一的缺点是你必须失去 AoT。是否可以/是否可以在组件级别禁用 AoT?如果我们可以在 AoT 中编译项目的大部分内容,但保留一个已定义的部分,那就太好了。
现在我的构建命令必须是:
ng build ProjectName --aot=false
ng build ProjectName --prod aot=false --build-optimizer=false