ChartJS v3 与 TypeScript 和 Webpack 的结合使用
Usage of ChartJS v3 with TypeScript and Webpack
直到 release 2.9.4, I used ChartJS in combination with @types/chart.js 才能获得 TypeScript 支持的所有类型。我使用 Webpack 和 TypeScript 的最小工作示例如下所示:
package.json:
{
"devDependencies": {
"@types/chart.js": "^2.9.31",
"ts-loader": "^8.1.0",
"typescript": "^4.2.3",
"webpack": "^5.30.0",
"webpack-cli": "^4.6.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"chart.js": "^2.9.4"
}
}
tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true,
},
}
webpack.config.json:
module.exports = {
entry: {
main: "./main.ts",
},
module: {
rules: [
{
test: /\.ts$/,
loader: "ts-loader",
exclude: /node_modules/,
},
],
},
};
main.ts:
import Chart from "chart.js";
new Chart("chart", {
type: 'line',
data: {
labels: ['Label 1', 'Label 2', 'Label 3'],
datasets: [{
label: "Dataset 1",
data: [1, 2, 3],
}]
},
});
index.html:
<!DOCTYPE html>
<html>
<body>
<canvas id="chart"></canvas>
<script src="main.js"></script>
</body>
</html>
但在更新到 version 3.0.0, this doesn't work anymore (I removed @types/chart.js because chart.js 后自该版本发布以来有自己的类型):
npx webpack serve --mode development
ERROR in [...]/chartjs/main.ts
./main.ts 3:4-9
[tsl] ERROR in [...]/chartjs/main.ts(3,5)
TS2351: This expression is not constructable.
Type 'typeof import("[...]/chartjs/node_modules/chart.js/types/index.esm")' has no construct signatures.
但是由于内置 class Chart
实际上有一个 constructor,我不太明白这个问题以及如何解决它。将 ChartJS 版本 3 与 Webpack 和 TypeScript 结合使用的预期方式是什么?
提前感谢您提供的任何提示,让我在这里找到正确的方向!
Chart.js3是tree-shakeable,所以需要导入注册你要用到的控制器,元素,秤和插件
Please take a look at Bundlers (Webpack, Rollup, etc.) from the Chart.js documentation.
直到 release 2.9.4, I used ChartJS in combination with @types/chart.js 才能获得 TypeScript 支持的所有类型。我使用 Webpack 和 TypeScript 的最小工作示例如下所示:
package.json:
{
"devDependencies": {
"@types/chart.js": "^2.9.31",
"ts-loader": "^8.1.0",
"typescript": "^4.2.3",
"webpack": "^5.30.0",
"webpack-cli": "^4.6.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"chart.js": "^2.9.4"
}
}
tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true,
},
}
webpack.config.json:
module.exports = {
entry: {
main: "./main.ts",
},
module: {
rules: [
{
test: /\.ts$/,
loader: "ts-loader",
exclude: /node_modules/,
},
],
},
};
main.ts:
import Chart from "chart.js";
new Chart("chart", {
type: 'line',
data: {
labels: ['Label 1', 'Label 2', 'Label 3'],
datasets: [{
label: "Dataset 1",
data: [1, 2, 3],
}]
},
});
index.html:
<!DOCTYPE html>
<html>
<body>
<canvas id="chart"></canvas>
<script src="main.js"></script>
</body>
</html>
但在更新到 version 3.0.0, this doesn't work anymore (I removed @types/chart.js because chart.js 后自该版本发布以来有自己的类型):
npx webpack serve --mode development
ERROR in [...]/chartjs/main.ts
./main.ts 3:4-9
[tsl] ERROR in [...]/chartjs/main.ts(3,5)
TS2351: This expression is not constructable.
Type 'typeof import("[...]/chartjs/node_modules/chart.js/types/index.esm")' has no construct signatures.
但是由于内置 class Chart
实际上有一个 constructor,我不太明白这个问题以及如何解决它。将 ChartJS 版本 3 与 Webpack 和 TypeScript 结合使用的预期方式是什么?
提前感谢您提供的任何提示,让我在这里找到正确的方向!
Chart.js3是tree-shakeable,所以需要导入注册你要用到的控制器,元素,秤和插件
Please take a look at Bundlers (Webpack, Rollup, etc.) from the Chart.js documentation.