Aurelia CLI can't import chart.js TypeScript
我在aurelia / aurelia-cli中使用打字稿。 我已经安装了npm
1 2 3 4 5 6 7 8 | "dependencies": [ ... { "name":"chartjs", "path":"../node_modules/chart.js/dist", "main":"Chart" }, ] |
我也输入了:
1 | typings install dt~chart.js --global --save |
现在,我尝试将其导入到我的组件之一中
1 | import * as Chart from 'chartjs' |
但是我得到了错误:
1 | Cannot find module 'chartjs'. |
我也尝试过:
1 2 3 4 5 6 | ... from 'chart' ... from 'Chart' ... from 'Chart.js' ... from 'chartjs' import * as chart ... import * as chartjs ... |
我可以通过更改import和aurelia.json文件来使其工作。
aurelia.json
1 2 3 4 | { "name":"chartjs", "path":"../node_modules/chart.js/dist/Chart.min" }, |
进口声明
1 | import 'chartjs'; |
您是否尝试过声明一个空模块?例如,在像
1 | declare module"chart.js"; |
我发现这是导入第三方库的良好起点。可以在这里找到更多信息:https://www.typescriptlang.org/docs/handbook/modules.html#working-with-other-javascript-libraries
如果没有错误,并且您可以使用普通的js
另外,如果您使用的是Typescript 2.0,我认为您应该安装这样的类型:
1 | npm install --save @types/chartjs |
我知道Chart.js可与Aurelia一起使用,因为我们在研讨会的演示应用程序之一中使用了它。在演示应用程序中,它是这样导入的:
另外,
1 2 3 4 5 | { "name":"chartjs", "path":"../node_modules/chart.js/Chart.min", "exports":"Chart" } |