【问题标题】:Uncaught ReferenceError: require is not defined with Dojo API未捕获的 ReferenceError:Dojo API 未定义 require
【发布时间】:2019-01-16 01:53:11
【问题描述】:

我已经看到此错误的多个化身,但没有看到与使用 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"and

<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>

【问题讨论】:

    标签: javascript html dojo require dojox.charting


    【解决方案1】:

    脚本标签第 6 行中存在类型错误的第一件事, 封闭标签脚本和脚本标签外的src属性,这就是为什么你有错误reuire不是......

    而且改正后还是会有一些错误,

    所以你需要修复导入

    'dojox/charting/PiePlot' 应替换为 'dojox/charting/plot2d/Pie'

    你需要在这里声明你的chartData

    如果您需要文件版本,请参阅GIST

    否则请参阅下面的工作 sn-p :

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多