在angular
我想懒惰加载像amcharts
这样的模块。我知道怎么做但是编辑器中的类型提示怎么样?
这是我的代码:
import {AfterViewInit, Component, NgZone, OnInit} from '@angular/core';
// import * as am4core from '@amcharts/amcharts4/core';
// import * as am4charts from '@amcharts/amcharts4/charts';
// import am4themes_animated from '@amcharts/amcharts4/themes/animated';
@Component({
selector: 'app-wykres',
templateUrl: './wykres.component.html',
})
export class WykresComponent implements OnInit, AfterViewInit {
private am4core: any;
private am4charts: any;
private am4themesAnimated: any;
chart: any;
constructor(private zone: NgZone) {
}
ngOnInit() {
}
ngAfterViewInit() {
this.zone.runOutsideAngular(() => {
this.loadAmcharts()
.then(() => {
console.log('loaded', this.am4charts, this.am4themesAnimated, this.am4core);
this.am4core.useTheme(this.am4themesAnimated);
let chart = this.am4core.create('chartdiv', this.am4charts.XYChart);
chart.paddingRight = 20;
let data = [];
let visits = 10;
for (let i = 1; i < 366; i++) {
visits += Math.round(
(Math.random() < 0.5 ? 1 : -1) * Math.random() * 10
);
data.push({
date: new Date(2018, 0, i),
name: 'name' + i,
value: visits
});
}
chart.data = data;
let dateAxis = chart.xAxes.push(new this.am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;
let valueAxis = chart.yAxes.push(new this.am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
valueAxis.renderer.minWidth = 35;
let series = chart.series.push(new this.am4charts.LineSeries());
series.dataFields.dateX = 'date';
series.dataFields.valueY = 'value';
series.tooltipText = '{valueY.value}';
chart.cursor = new this.am4charts.XYCursor();
let scrollbarX = new this.am4charts.XYChartScrollbar();
scrollbarX.series.push(series);
chart.scrollbarX = scrollbarX;
this.chart = chart;
});
});
}
private async loadAmcharts(): Promise<any> {
this.am4core = await import('@amcharts/amcharts4/core');
this.am4charts = await import('@amcharts/amcharts4/charts');
this.am4themesAnimated = await import('@amcharts/amcharts4/themes/animated').then(resp => {
return resp.default;
});
}
}
在loadAmcharts
我正在加载模块并将它们分配给属性。但那些都是任何类型的。
如果我发表评论:
import * as am4core from '@amcharts/amcharts4/core';
import * as am4charts from '@amcharts/amcharts4/charts';
import am4themes_animated from '@amcharts/amcharts4/themes/animated';
amchart
是主要的捆绑。我可以延迟加载模块并在编辑器中有类型提示吗?
这是一个很好的问题。
这更像是一个TypeScript / IDE问题,例如:我能够找到这个issue,它描述了提示静态/动态导入之间的区别:
静态的:
[...] .d.ts文件是运行时存在的静态表示,可以从TypeScript代码生成,也可以手动编写,而不是用TS编写的库。
动态:
并非一切都可以被推断 - 捕获精确的行为需要从字面上运行您的程序。
那么我们如何在编码时强制TS知道我们的类型是什么? TypeScript 2.9 introduced a feature "import
types",它适用于您的IDE,以下是它如何在您的情况下工作:
private am4core: typeof import ('@amcharts/amcharts4/core');
private am4charts: typeof import ('@amcharts/amcharts4/charts');
private am4themesAnimated: typeof import ('@amcharts/amcharts4/themes/animated').default;
现在你应该得到代码提示:
const core = this.am4core;
const charts = this.am4charts;
const animatedTheme = this.am4themesAnimated;
console.log('loaded', core, charts, animatedTheme);
core.useTheme(animatedTheme);
const chart = core.create('chartdiv', charts.XYChart);
截图(我使用VS代码):
希望这可以帮助!