如何将导出模块包含到angular2应用程序中?
我尝试将以下脚本添加到index.html中,但没有任何反应。
<script src="http://code.highcharts.com/modules/exporting.js"></script>发布于 2016-11-01 00:40:29
从Angular CLI1.0-rm1和最新的angular2-highcharts开始,这对我来说是有效的(另请参阅推荐解决方案https://github.com/gevgeny/angular2-highcharts/issues/156的正在进行的讨论):
npm install --save angular2-highcharts
npm install --save @types/highcharts在typings.d.ts中添加:declare var require: any;
然后在app.module.ts中:
import { ChartModule } from 'angular2-highcharts';
import {HighchartsStatic} from 'angular2-highcharts/dist/HighchartsService';
export function highchartsFactory() {
var hc = require('highcharts');
var hcm = require('highcharts/highcharts-more');
var exp = require('highcharts/modules/exporting');
hcm(hc);
exp(hc);
return hc;
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
ChartModule
],
providers: [{provide: HighchartsStatic, useFactory: highchartsFactory}],
bootstrap: [AppComponent]
})
export class AppModule { }早期版本的旧答案:
如果你使用angular-cli (webpack)和angular2-highcharts,这对我很有效:
import {Highcharts} from 'angular2-highcharts';
require('highcharts/modules/exporting')(Highcharts);发布于 2016-08-03 20:32:19
创建一个指令HighchartsExporting,例如,下面是一个用于Highcharts的指令:
import {Directive, ElementRef, Input, OnDestroy} from '@angular/core';
@Directive({
selector: '[ng2-highcharts]',
exportAs: 'ng2Highcharts'
})
export class Ng2Highcharts implements OnDestroy {
hostElement: ElementRef;
pChart: HighchartsChartObject;
constructor(ele: ElementRef) {
this.hostElement = ele;
}
@Input('ng2-highcharts') set options(opt: HighchartsOptions) {
if (!opt) {
console.log('No valid options...');
console.log(opt);
return;
}
if (opt.series || opt.data) {
if (this.pChart) {
this.pChart.destroy();
}
if (!opt.chart) {
opt.chart = {};
}
opt.chart.renderTo = this.hostElement.nativeElement;
this.pChart = new Highcharts.Chart(opt);
} else {
console.log('No valid options...');
console.dir(opt);
}
}
public get chart() : HighchartsChartObject {
return this.pChart;
}
ngOnDestroy() {
if (this.pChart) {
this.pChart.destroy();
}
}
}https://stackoverflow.com/questions/38742480
复制相似问题