dojo parser and a TypeError using ArcGIS JS API

2019-04-10 19:20发布

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条回答
太酷不给撩
2楼-- · 2019-04-10 19:35

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.

查看更多
登录 后发表回答