Angular ComponentFactoryResolver中的解析器做什么?
类中的关注点太多,无法创建(工厂)和解决(解析器)。Angular2的用途和作用是什么?您可以使用它创建动态组件,它专门用于 用于获取我们要注入的组件的工厂 当您将组件名称传递到工厂时,它将返回页面的Angular ComponentFactoryResolver中的解析器做什么?,angular,Angular,类中的关注点太多,无法创建(工厂)和解决(解析器)。Angular2的用途和作用是什么?您可以使用它创建动态组件,它专门用于 用于获取我们要注入的组件的工厂 当您将组件名称传递到工厂时,它将返回页面的entryComponents部分中定义的组件的相应工厂 例如,如果您有一个包含一系列不同图形的仪表板,您可能希望使用不同的图形显示相同的数据,例如直线、条形 您可以使用DynamicComponent插入组件,例如BarChartComponent或LineGraphComponent,具体取决
entryComponents
部分中定义的组件的相应工厂
例如,如果您有一个包含一系列不同图形的仪表板,您可能希望使用不同的图形显示相同的数据,例如直线、条形
您可以使用DynamicComponent
插入组件,例如BarChartComponent
或LineGraphComponent
,具体取决于所需内容
此工厂用于解析该组件
对象到传递.ts
动态组件
dynamic.component.html
这要归功于一篇关于它是如何实现的博文
希望这会有所帮助。这符合它的目的,但不是它内部的功能。
public standard: any =
{
Component: LineGraphComponent,
Inputs: {
GraphData: {
//...graph data
}
}
};
import { Component, Input, ViewContainerRef, ViewChild, ReflectiveInjector, ComponentFactoryResolver } from "@angular/core";
import { BarChartComponent } from "./barchart.component";
import { LineGraphComponent } from "./linegraph.component";
@Component({
selector: "hfa-dynamic-search",
entryComponents: [BarChartComponent , LineGraphComponent],
templateUrl: "app/app.component.html",
styleUrls: ["app/app.component.css"]
})
export calss DynamicComponent {
@ViewChild("dynamicComponent", { read: ViewContainerRef }) protected dynamicComponent: ViewContainerRef;
@Input()
set componentData(data: any) {
...
//Creates a factory out of the passed in component name
let factory = this.resolver.resolveComponentFactory(data.Component);
//Creates the component out of the factory with the input values we want to inject
let component = factory.create(injector);
//Inserts it into the view
this.dynamicComponent.insert(component.hostView);
...
}
constructor(private resolver: ComponentFactoryResolver) { }
}
<div #dynamicComponent></div>