【发布时间】:2019-06-18 05:52:31
【问题描述】:
我有四个独立的组件,每个 (chart.js) 中有四种不同类型的图表,其中还包括图表的设置。我想根据不同组件的后端响应包含特定图表。
我使用 @ViewChild 访问特定组件的方法/变量,并在 dashboard.component.html 中包含组件的 HTML。我正在使用 *ngFor 因为我正在从后端获得响应,然后使用 *ngIf 来调用/包括特定的
我也尝试过 ComponentFactory 创建特定组件,但由于无法访问包含组件的方法而放弃了这种方法。
<--HTML CODE-->
<div class="col-xl-6 mb-4" *ngFor="let graphs of
dashboardRes?.data?.dashboard_graphs">
<app-sales-pipeline *ngIf="graphs?.chart_detail?.type ==
'horizontal_bar_chart'"></app-sales-pipeline>
<app-line-chart *ngIf="graphs?.chart_detail?.type == 'line_chart'"></app-
line-chart>
<app-gauge-chart *ngIf="graphs?.chart_detail?.type == 'guage_chart'">
</app-gauge-chart>
</div>
<--TS CODE-->
@ViewChild(LineChartComponent) lineChart: LineChartComponent;
@ViewChild(BarChartComponent) barChart: BarChartComponent;
@ViewChild(GaugeChartComponent) gaugeChart: GaugeChartComponent;
// Not using this approach as i was unable to access included component's methods.
createBarChart(): void {
const factory: ComponentFactory<any> =
this.resolver.resolveComponentFactory(BarChartComponent);
this.componentRef = this.container.createComponent(factory);
}
我想在应用程序中任何需要的地方动态地包含图表组件。我还想调用他们的特定方法来通过 API 检索图表数据。图表类型、图表设置和图表数量将出现在 Dashboard API 中,其中图表数据应带有其特定组件(因为我也将在报告页面中包含这些图表 - 使用不同的数据)。我主要关心的是使用 API 更改特定图表的设置并保存它们(设置来自仪表板 API)
【问题讨论】: