不允许多个构造函数实现

时间:2018-09-17 06:32:36

标签: angular typescript d3.js constructor

我需要在一个页面中显示多个d3图表。在为一张图表添加代码时,它可以工作。 但是会显示多个图表的错误。

bardata: Observable<ChartModel>;


constructor(private http: HttpClient) {
    this.bardata = this.http.get<ChartModel>('./assets/mocdata/data.json');
    console.log('sd', this.bardata);
}


areadata: Observable<ChartModel>;


constructor(private http: HttpClient) {
    this.areadata = this.http.get<ChartModel>('./assets/mocdata/data.json');
    console.log('sd', this.areadata);
}

请帮助我清除构造函数问题。

谢谢。

2 个答案:

答案 0 :(得分:1)

只需为每个图表创建方法,然后在构造函数中调用它,如下所示:

bardata: Observable<ChartModel>;
areadata: Observable<ChartModel>;

constructor(private http: Httpclient) {
  this.getBarData();
  this.getAreaData();
}

getBarData() {
  this.bardata = this.http.get<ChartModel>('./assets/mocdata/data.json');
  console.log('sd', this.bardata);
}

getAreaData() {
  this.areadata = this.http.get<ChartModel>('./assets/mocdata/data.json');
  console.log('sd', this.areadata);
}

答案 1 :(得分:0)

您可以做类似的事情

bardata: Observable<ChartModel>;
areadata: Observable<ChartModel>;

constructor(private http: HttpClient) {
    this.bardata = this.http.get<ChartModel>('./assets/mocdata/data.json');
    console.log('sd', this.bardata);

    this.areadata = this.http.get<ChartModel>('./assets/mocdata/data.json');
    console.log('sd', this.areadata);
}