angular - 基于 HTTP 调用的 Angular 动态组件
问题描述
动态创建组件模板和 Typescript 代码。
我正在尝试动态生成组件,HTTP 服务调用向我们发送 HTML 模板和 Typescript 代码。在这里,我们能够动态设置 HTMl 模板,但不能动态设置 typescript 代码,无法弄清楚如何动态设置 typescript 代码。
* 请在下面找到基于 HTTP 调用呈现 HTML 模板的代码。*
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { Component, ViewChild, ViewContainerRef, Compiler, Injector, NgModule, NgModuleRef, ComponentFactoryResolver, OnInit } from "@angular/core";
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('vc', { read: ViewContainerRef }) _container: ViewContainerRef;
constructor(private http: HttpClient, private _compiler: Compiler, private componentFactoryResolver: ComponentFactoryResolver,
private _injector: Injector,
private _m: NgModuleRef<any>) {
}
ngOnInit() {
return this.http.get("https://raw.githubusercontent.com/ketan-gote/stackblitz-demo/master/template.txt", { responseType: 'text' }).subscribe((txt: any) => {
console.log(txt);
const tmpCmp = Component({ template: txt, selector: 'abcd' })(class {
});
const tmpModule = NgModule({ imports: [HttpClientModule], declarations: [tmpCmp], entryComponents: [tmpCmp] })(class {
});
this._compiler.compileModuleAsync(tmpModule)
.then((moduleFactory) => {
debugger;
const resolver = moduleFactory.create(this._injector).componentFactoryResolver;
const f = resolver.resolveComponentFactory(tmpCmp);
const cmpRef = f.create(this._injector, [], null, this._m);
this._container.insert(cmpRef.hostView);
})
});
}
}
这是一个工作链接!
* 问题 现在,当我们尝试获取 TS 代码作为响应的一部分时,它会给出错误core.js:12501 错误类型错误:无法读取未定义的属性“__annotations__”*
请在下面找到呈现 TS 的代码
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { Component, ViewChild, ViewContainerRef, Compiler, Injector, NgModule, NgModuleRef, ComponentFactoryResolver, OnInit } from "@angular/core";
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild('vc', { read: ViewContainerRef }) _container: ViewContainerRef;
constructor(private http: HttpClient, private _compiler: Compiler, private componentFactoryResolver: ComponentFactoryResolver,
private _injector: Injector,
private _m: NgModuleRef<any>) {
}
ngOnInit() {
return this.http.get("https://raw.githubusercontent.com/ketan-gote/stackblitz-demo/master/ts.txt", { responseType: 'text' }).subscribe((txt: any) => {
console.log(txt);
const tmpCmp = Component({ template: "<h1>HEAD</h1>", selector: 'abcd' })(txt);
const tmpModule = NgModule({ imports: [HttpClientModule], declarations: [tmpCmp], entryComponents: [tmpCmp] })(class {
});
this._compiler.compileModuleAsync(tmpModule)
.then((moduleFactory) => {
debugger;
const resolver = moduleFactory.create(this._injector).componentFactoryResolver;
const f = resolver.resolveComponentFactory(tmpCmp);
const cmpRef = f.create(this._injector, [], null, this._m);
this._container.insert(cmpRef.hostView);
})
});
}
}
这 是无效的代码链接!
解决方案
我知道已经很晚了,但这可以通过“评估”功能来完成。
从此改行
const tmpCmp = Component({ template: "<h1>HEAD</h1>", selector: 'abcd' })(txt);
对此
const tmpCmp = Component({ template: "<h1>HEAD</h1>", selector: 'abcd' })(eval('(' +txt+ ')'));
推荐阅读
- php - if条件如何从函数php返回值
- c# - 无法在 c#view 中将 Json 子项日期时间与当前日期时间进行比较
- r - R中的向量索引
- python - 是否可以将一个 QGraphicsPixmapItem 附加到另一个?
- ruby-on-rails - 按钮下载电子邮件中的附件
- haskell - Haskell - 延迟评估是否会导致此文件夹停止评估所有功能
- class - C ++:在基类中定义的静态向量在main中失去了它的价值
- ios - Google Maps 为点击和未选择状态绘制不同的标记图像
- python - 超级向量机:多标签还是多类?
- workflow - Netsuite 发送有关工作订单创建的电子邮件