【问题标题】:Third party JS in Angular2 typescriptAngular2打字稿中的第三方JS
【发布时间】:2016-01-07 10:40:09
【问题描述】:

我是 angular 和 typescript 的新手,所以这可能是非常基础的。

我正在尝试在模板中制作带有图表(使用 Chart.js)的 angular2 组件。

我意识到正在开发一个chart directive,它专门使用 Chart.JS,但我想了解如何执行此操作,因为它无疑会在指令不可用的情况下出现。

到目前为止,我已经尝试在模板中做这个简单的事情:

<canvas id="chart"></canvas>
<script> 
$(function () {
//instantiate chart on $("#chart")
});
</script>

但是当模板已经被 angular2 加载时,这个 javascript 甚至不会运行。

我该怎么做?

【问题讨论】:

  • 你想做什么?这与打字稿有什么关系?您的问题和代码没有显示任何关于打字稿的内容..
  • 你需要在 typescript 中为chart.js 库编写接口,但可能已经做了一些,但你已经走了多远?
  • 不@Dynde,.ts 文件需要强类型,js 库不是,所以你使用一个接口:这可能对你有用:github.com/DefinitelyTyped/DefinitelyTyped/blob/master/chartjs/… on另一个注意事项,有像说chartLib: any 这样的“黑客”,所以 any 会绕过强类型,但这确实违背了目的。
  • 好的 - 谢谢。我将尝试阅读更多关于 typescript 接口的信息。我想我只是不太明白那个界面是如何知道chart.js的来源的——我才刚刚开始打字
  • @Dynde 一个接口不知道!这就是你定义接口的原因,并确保你做对了! :) 我发给你的那个库有很多 TS 的第 3 方定义!

标签: angular typescript


【解决方案1】:

好的 - 在@Pogrindis 的帮助下,我想我找到了一个可用的、不太复杂的解决方案。

通过简单地从here 添加chart.js 的类型定义并在自定义指令中引用它,我终于有了这个:

chart.directive.ts

/// <reference path="../../typings/chartjs/chart.d.ts" />

import {Directive, ElementRef, Renderer, Input} from 'angular2/core';
@Directive({
    selector: '[chart]'
})
export class ChartDirective {
    constructor(el: ElementRef, renderer: Renderer) {
        //el.nativeElement.style.backgroundColor = 'yellow';
        var data = {
            labels: ["January", "February", "March", "April", "May", "June", "July"],
            datasets: [
                {
                    label: "My First dataset",
                    fillColor: "rgba(220,220,220,0.2)",
                    strokeColor: "rgba(220,220,220,1)",
                    pointColor: "rgba(220,220,220,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(220,220,220,1)",
                    data: [65, 59, 80, 81, 56, 55, 40]
                },
                {
                    label: "My Second dataset",
                    fillColor: "rgba(151,187,205,0.2)",
                    strokeColor: "rgba(151,187,205,1)",
                    pointColor: "rgba(151,187,205,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(151,187,205,1)",
                    data: [28, 48, 40, 19, 86, 27, 90]
                }
            ]
        };
        var ctx: any = el.nativeElement.getContext("2d");
        var lineChart = new Chart(ctx);
        ////var lineChartOptions = areaChartOptions;
        ////lineChartOptions.datasetFill = false;
        lineChart.Line(data);
    }
}

app.component.ts

import {Component} from 'angular2/core';
import {ChartDirective} from './chart.directive';

@Component({
    directives: [ChartDirective],
    selector: 'chart-graph', 
    templateUrl: '/js/app/template.html'
})

export class AppComponent { }

和template.html:

<canvas id="myChart" chart width="400" height="400"></canvas>

【讨论】:

  • 感谢您的提问,这正是我想要的。我有一个大致的想法为什么你需要DefiniteTyped,但是我对如何实现它有点困惑。 1. git clone 绝对类型,2. 然后在 index.html 中引用它&lt;script src="DefinitelyTyped/chartjs/chart.d.ts"&gt;&lt;/script&gt;?可以详细说明您的答案。我理解的所有其他内容,即使是 Angular2!
  • 好吧,这就是诀窍 /// 我不知道这是 Angular2 中的命令或行,出于某种原因,我认为这是一条注释行。无论如何,请确保这个答案可以让您在 Chart.js 和 Angular2 上运行!谢谢@dynde
  • @Dynde 您在回答中提到的类型中使用了哪个版本的 charts.js?
  • @Anmol 哦,该死,这么久了,我真的不知道,而且项目已经被放弃,所以我无法挖掘它 - 抱歉。
  • @Dynde 不用担心。我想到了。此处提及的类型适用于 1x 版本。这些对 2x 版本无效。仍在等待 2x 版本的输入。该问题已在 Github 上公开。 github.com/chartjs/Chart.js/issues/1572
猜你喜欢
  • 2017-10-18
  • 2015-11-25
  • 2018-01-06
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多