gpt4 book ai didi

plot - 如何将 chartjs 作为 es6 模块导入?

转载 作者:行者123 更新时间:2023-12-04 16:06:46 25 4
gpt4 key购买 nike

我有几个用于基于代理的建模的存储库,并且想使用绘图/图表库来查看它们创建的数据。

我使用过chartjs,但我不确定是否可以导入它。我没有使用 webpack browserify 等,只是使用 vanilla es6。我确实使用 rollup 在我的 repos 中构建变体:cjs 用于节点、单文件 es6 和 iife 用于脚本。但是除了 npm run 脚本之外没有构建系统。
https://medium.com/@backspaces/es6-modules-part-1-migration-strategy-a48de0b7f112

鉴于极简主义的环境:

* How to I import chart.js? I'm currently using their CDN
* If that is impossible, what other charting library *is* importable?

最佳答案

我对整个 JS 模块很陌生,但遇到了完全相同的问题。
我使用以下代码让它工作:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Load Chart.js as ES2015 module</title>
<script type="module">
import 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js'

const canvas = document.getElementById("chart");
let chart = new Chart(canvas, {
type: "line",
data: {
labels: ["1", "2", "3", "4", "5"],
datasets: [{
label: "Series 1",
data: [1, 2, 3, 2, 1]
}]
}
});
</script>
</head>
<body>
<canvas id="chart"></canvas>
</body>
</html>


我现在面临的问题是 TypeScript 编译器给了我以下错误:
'Chart' refers to a UMD global, but the current file is a module. Consider adding an import instead.

但是如果你不使用 TypeScript 并且上面的 JS 代码在浏览器中工作,这不是问题。

关于plot - 如何将 chartjs 作为 es6 模块导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48410525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com