How to import plotly.js into TypeScript?
我正在尝试将
1 | import 'plotly.js'; |
可以,但是我在
1 | error TS2304: Cannot find name 'Plotly' |
当我尝试
1 | import Plotly = require('plotly.js'); |
我越来越
1 | error TS2307: Cannot find module 'plotly.js'. |
有一个可用于plotly.js的"绝对类型"版本(Github链接)。这使您可以在Typescript中使用纯Javascript版本。请按照以下步骤设置您的环境:
1 2 | npm install --save plotly.js npm install --save @types/plotly.js |
然后,您可以在代码中执行以下操作(示例取自Github):
1 2 3 4 5 6 7 8 9 10 11 | import * as Plotly from 'plotly.js'; const data: Plotly.BarData[] = [ { x: ['giraffes', 'orangutans', 'monkeys'], y: [20, 14, 23], type: 'bar' } ]; Plotly.newPlot('test', data); |
最后,您需要使用
现在也适用于ionic 2!确保您手动添加以下软件包:
1 2 | npm install --save glslify npm install --save mapbox-gl |
最终得到:
1 2 | declare function require(moduleName: string): any; var Plotly = require('plotly.js/lib/index-basic.js'); |
(由于我只需要基本功能,请参考index-basic.js,将index.js用于完整的库。)
编辑(2018年2月):
现在首选的方法是将路径添加到
1 2 3 4 5 6 7 | "compilerOptions": { "paths": { "plotly.js": [ "../node_modules/plotly.js/lib/core.js" ] } } |
然后像这样导入
1 | import * as Plotly from 'plotly.js'; |
注意:在仅包含
(我在Angular CLI中使用它-Angular 5.2和Typesript 2.7.1)
TS支持
1 | npm install --save-dev @types/plotly.js` |
要么
1 | yarn add --dev @types/plotly.js` |
和
1 | import { PlotData } from"plotly.js"; // or whatever you need |
不要将@types安装在依赖项中,因为它们仅用于开发目的。
问题
由于您添加了plotly.js的类型,因此您希望从plotly.js而不是特定模块中导入内容。这一切都很好,直到您想将其与react即使用一起使用,即react-plotly + plotly.js ??
我知道您没有明确提及React,但是我认为您或其他遇到类似问题的人可能会觉得这很有用。
我不确定您的捆绑方法,但是,我使用的是Webpack 4,目前正在研究react-plotly + plotly.js-basic-dist组合。
可能的组合是:
- react-plotly + plotly.js-basic-dist –我的选择。您可以根据需要添加更多捆绑包
- 反应图+ CDN
- react-plotly +自定义包
有关自定义绘图包的更多信息。
这使捆束的尺寸大大减小,太阳再次照在我们身上。
但...
由于此方法需要工厂和自定义dist,因此在撰写本文时,这些没有类型
1 2 3 | import Plotly from"plotly.js-basic-dist"; import createPlotlyComponent from"react-plotly.js/factory"; const Plot = createPlotlyComponent(Plotly); |
固定
N.B.以下方法确实修复了上述导入的类型,但是我仍然编译失败,但是按照可接受的答案,我无法解决该问题:
希望您有更好的体验!
更新
我解决了编译问题。这是因为react-plotly.js被捆绑在" dependencies"中。对于我来说,供应商部门的处理方式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Given an array of files this will produce an object which contains the values for the vendor entry point */ function makeVendorEntry(config) { const packageJson = require('../package.json'); const vendorDependencies = Object.keys(packageJson['dependencies']); const vendorModulesMinusExclusions = vendorDependencies.filter(vendorModule => config.mainModules.indexOf(vendorModule) === -1 && config.modulesToExclude.indexOf(vendorModule) === -1); return vendorModulesMinusExclusions; } exports.makeVendorEntry = makeVendorEntry; |
相反,我将
如果要导入的npm库本身未提供类型声明,则必须自己导入它们。首选工具是打字。
出于阴谋,似乎已经有人声明了文件。因此,一旦安装了类型(
注意
1 2 | // app.ts let myPlotlyConfig: PlotlyConfig |
使用NPM安装plotly.js:
1 | npm install --save plotly.js |
然后在组件或服务中要使用导入的任何地方,例如:
1 2 3 4 5 6 | import Plotly from 'plotly.js/lib/core'; Plotly.register([ require('plotly.js/lib/heatmap'), require('plotly.js/lib/bar'), require('plotly.js/lib/pie') // can include as many plot types as you want ]); |
使用plotly lib,如下所示:
1 2 3 4 5 6 7 8 9 | const data: Plotly.BarData[] = [ { x: ['giraffes', 'orangutans', 'monkeys'], y: [20, 14, 23], type: 'bar' } ]; Plotly.newPlot('test', data); |
plotly.js basic&Co.的类型
您还可以将类型与部分npm捆绑包(例如基本或笛卡尔)一起使用,从
1 2 | npm i plotly.js-basic-dist npm i -D @types/plotly.js |
在tsconfig.json中添加以下内容:
1 2 3 4 5 6 7 8 9 | "compilerOptions": { // make aliases absolute imports relative to the project root "baseUrl":"./", "paths": { "plotly.js-basic-dist": [ "node_modules/@types/plotly.js" ] } } |
在
- baseUrl
- 路径