【问题标题】:dojo parser and a TypeError using ArcGIS JS APIdojo 解析器和使用 ArcGIS JS API 的 TypeError
【发布时间】:2013-10-18 19:30:20
【问题描述】:

我收到此错误:

TypeError: 'undefined' 不是函数(正在评估 'parser.parse()')

这是我的代码。

<script> 
    require([
      "esri/map", 
      "esri/layers/FeatureLayer", 
      "esri/dijit/Legend",
      "esri/digit/HomeButton",
      "dojo/_base/array", 
      "dojo/parser",
      "dijit/layout/BorderContainer", 
      "dijit/layout/ContentPane", 
      "dijit/layout/AccordionContainer", 
      "dojo/domReady!"
], function(
  Map, FeatureLayer, Legend, arrayUtils, parser, HomeButton
) {
    parser.parse();

    var map = new Map("map", {
        basemap: "streets",
        center: [-87.702733, 41.998508],
        zoom: 15
    });

    var home = new HomeButton ({
        map: map
    }, "HomeButton");
    home.startup();

  var circuits = new FeatureLayer("http://54.243.188.50:6080/arcgis/rest/services/TYLIN/Streetlights_Pilot/MapServer/6", {
    mode: FeatureLayer.MODE_ONDEMAND,
    outFields:["*"]
  });

  var power = new FeatureLayer("http://54.243.188.50:6080/arcgis/rest/services/TYLIN/Streetlights_Pilot/MapServer/2", {
    mode: FeatureLayer.MODE_ONDEMAND,
    outFields:["*"]
  });


  //add the legend
  map.on("layers-add-result", function (evt) {
    var layerInfo = arrayUtils.map(evt.layers, function (layer, index) {
      return {layer:layer.layer, title:layer.layer.name};
    });
    if (layerInfo.length > 0) {
      var legendDijit = new Legend({
        map: map,
        layerInfos: layerInfo
      }, "legendDiv");
      legendDijit.startup();
    }
  });

  map.addLayers([circuits, power]);
});

</script>

我已经能够通过将 HomeButton 部分与 Legend 部分分开来运行它。在拐角处,我必须调用 dojo.parser.parse() ——但这不适用于 Legend 部分。

我不太了解 dojo/parser 是如何工作的,所以我很难理解为什么有些部分需要 dojo.parser.parse() 而其他部分需要 parser.parse()。

【问题讨论】:

    标签: javascript parsing dojo arcgis


    【解决方案1】:

    您的require() 有问题。 dojo/parser 是您导入的第 6 个模块,然而,名为 parser 的参数位于第 5 位,因此它们不匹配。您导入的模块的顺序必须与您使用它们的参数相同,因此这是正确的方法:

    require([
          "esri/map", 
          "esri/layers/FeatureLayer", 
          "esri/dijit/Legend",
          "esri/digit/HomeButton",
          "dojo/_base/array", 
          "dojo/parser",
          "dijit/layout/BorderContainer", 
          "dijit/layout/ContentPane", 
          "dijit/layout/AccordionContainer", 
          "dojo/domReady!"
    ], function(
      Map, FeatureLayer, Legend, HomeButton, arrayUtils, parser
    ) {
    

    如您所见,我将 HomeButton 切换到另一个位置,现在模块与参数匹配。

    • esri/map(第一个模块):Map(函数中的第一个变量)
    • esri/layers/FeatureLayer(第二个模块):FeatureLayer(第二个参数)
    • esri/dijit/Legend(第三个模块):Legend(第三个参数)
    • esri/dijit/HomeButton(第4个模块):HomeButton(第4个参数)
    • dojo/_base/array(第5个模块):arrayUtils(第5个参数)
    • dojo/parser(第6个模块):parser(第6个参数)

    如果您将它们分开,您的代码之所以有效,是因为您可能修复了您的require()dojo.parser.parse() 工作的原因是因为它是旧的不推荐使用的遗留代码,它不使用 require 回调的参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多