我有两个javascript文件baseChart.js和chart.min.js。我不想将我的chart.min.js包含到index.html文件中。我只想在index.html中包含baseChart.js。
`var s = document.createElement("script");
s.setAttribute("src","js/Chart.min.js");
document.head.appendChild(s);
class BaseChart{
constructor(area, settings) {
this.area = area;
this.settings = settings;
}
a() {
new Chart(this.area, this.settings);
console.log("BaseChart");
}
}
class BarChart extends BaseChart {
constructor(area, settings) {
super(area, settings);
}
/* constructor(ctx) {
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [65, 59, 80, 81, 56, 55, 40],
spanGaps: false,
},
{
label: "Second",
data: [12, 56, 45, 23, 78, 23, 90],
}
]
};
var lineChart = new Chart(ctx, {
type: 'line',
data: data
});
} */
b() {
super.a();
}
}`
目前,这是baseChart.js文件,当我尝试运行此代码时,我收到错误"图表未定义,因为我使用了'新图表(this.area,this。设置);'并且在我调用此方法之前未初始化chart.min.js."。我想在初始化这个类和方法之前初始化chart.min.js。我使用了chart.js库。那么,我该怎么办?
答案 0 :(得分:0)
我建议您通过npm
安装chart.jsnpm install chart.js --save
然后将其导入:
// ES6
import Chart from 'chart.js';
我假设你正在使用像webpack这样的捆绑器来使代码在上面工作。如果不是,则必须在baseChart.js
之前的索引文件中包含chart.min.js