Uncaught ReferenceError: require is not defined with Dojo API

Uncaught ReferenceError: require is not defined with Dojo API

我已经看到此错误的多个版本,但没有看到与使用 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 行中存在类型错误, closed tag script and src attrib outside the script tag , 那为什么你有错误 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>