javascript - 未捕获的 ReferenceError:Dojo API 未定义 require
问题描述
我已经看到了这个错误的多个化身,但没有看到与使用 Dojo API/CDN 直接相关的答案。我只是通过一个快速的 Dojo 图表教程来了解如何正确应用饼图。我正在使用简单的说明来设置一个可以用来从本地测试的网页(见下文)。每次我启动.html
文件时,我都会收到错误 - Uncaught ReferenceError: require is not defined
. 之前的所有答案都指向src
错误,无论是cdn、api还是文件路径。我尝试了多种 CDN 和配置,包括src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"
和
<script data-dojo-config="async: 1"
src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>`,
但是我仍然遇到相同的错误(这些直接来自文档)。关于导致此错误的原因以及如何解决它以测试我的简单饼图的任何建议?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial: Pie Chart!</title>
<script> src="//ajax.googleapis.com/ajax/libs/dojo/1.13.0/dojo/dojo.js"></script>
</head>
<body>
<script>
require([
'dojo/dom',
'dojo/dom-construct',
'dojox/charting/Chart',
'dojox/charting/themes/Claro',
'dojox/charting/PiePlot'
], function (dom, domConstruct, Chart, theme, PiePlot) {
var pieChart = new Chart("chartNode");
// Set the theme
pieChart.setTheme(theme);
// Add the only/default plot
pieChart.addPlot("default", {
type: PiePlot, // our plot2d/Pie module reference as type value
radius: 200,
fontColor: "black",
labelOffset: -20
});
// Add the series of data
pieChart.addSeries("January",chartData);
// Render the chart!
pieChart.render();
});
</script>
<div id="chartNode" style="width: 550px; height: 550px;"></div>
</body>
</html>
解决方案
首先,您的脚本标签第 6 行、封闭标签脚本和脚本标签外的 src 属性中存在类型错误,这就是为什么您有错误 reuire 不是...
同样在更正之后你仍然会有一些错误,
所以你需要修复导入
'dojox/charting/PiePlot'
应该替换为'dojox/charting/plot2d/Pie'
你需要在chartData
这里声明你的,
如果您需要文件版本,请参阅此GIST
否则请参阅下面的工作片段:
require([
'dojo/dom',
'dojo/dom-construct',
'dojox/charting/Chart',
'dojox/charting/themes/Claro',
'dojox/charting/plot2d/Pie'
], function(dom, domConstruct, Chart, theme, PiePlot) {
chartData = [
{ y: 389, text: "data1 " },
{ y: 125, text: "data 2" },
{ y: 285, text: "data 3" },
{ y: 193, text: "data 4" },
{ y: 21, text: "No data" }
];
var pieChart = new Chart("chartNode");
// Set the theme
pieChart.setTheme(theme);
// Add the only/default plot
pieChart.addPlot("default", {
type: PiePlot, // our plot2d/Pie module reference as type value
radius: 200,
fontColor: "black",
labelOffset: -20
});
// Add the series of data
pieChart.addSeries("January", chartData);
// Render the chart!
pieChart.render();
});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dojo/dojo.js"></script>
<div id="chartNode" style="width: 550px; height: 550px;"></div>
推荐阅读
- powershell - 如何从PowerShell中的格式化文本创建对象
- python - 如何将 functools.partial 用于类方法?
- php - Wordpress 在使用 CPT 和分页时提供自定义 404.php(除第一页外的所有页面)
- python - 比较 2 个相同的音频文件以在 python 中找到它们之间的延迟
- angular - 实现以验证动态复选框
- python - 熊猫:带有方程式的Excel单元格在熊猫read_excel()中给出“0”
- android - 添加 google admob 依赖项后我无法构建我的项目
- c# - 访问 xamarin.form Android 10 sd 卡
- laravel - 在 Datatables 中添加按钮 // 打开按钮以打开另一个布局
- reactjs - 在反应中使用 FormData() 不断返回 null