dojo parser and a TypeError using ArcGIS JS API

半城伤御伤魂 提交于 2019-12-06 07:58:32

问题


I'm getting this error:

TypeError: 'undefined' is not a function (evaluating 'parser.parse()')

Here's my code.

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

I've been able to get this to run by splitting out the HomeButton part separate from the Legend part. In the comer, I have to call dojo.parser.parse() -- but that doesn't work with the Legend part.

I don't really understand how the dojo/parser works so it's difficult for me to understand why some parts require dojo.parser.parse() and others require parser.parse().


回答1:


There is a problem in your require(). dojo/parser is the 6th module you import, however, the parameter named parser is on the 5th place, so they don't match. The order of the modules you import must be the same as the parameters you use them in, so this is the correct way:

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

As you see, I switched HomeButton to another place and now the modules match the parameters.

  • esri/map (1st module): Map (1st variable in function)
  • esri/layers/FeatureLayer (2nd module): FeatureLayer (2nd parameter)
  • esri/dijit/Legend (3rd module): Legend (3rd parameter)
  • esri/dijit/HomeButton (4th module): HomeButton (4th parameter)
  • dojo/_base/array (5th module): arrayUtils (5th parameter)
  • dojo/parser (6th module): parser (6th parameter)

The reason why your code works if you seperate them is because you probably fix your require(). The reason why dojo.parser.parse() works is because it's old deprecated legacy code which doesn't use the parameter of the require callback.



来源:https://stackoverflow.com/questions/19305924/dojo-parser-and-a-typeerror-using-arcgis-js-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!