dojo 解析器和使用 ArcGIS JS API 的 TypeError [英] dojo parser and a TypeError using ArcGIS JS API

查看:23
本文介绍了dojo 解析器和使用 ArcGIS JS API 的 TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

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

TypeError: 'undefined' is not a function (evaluating '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 部分.

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.

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

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().

推荐答案

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

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

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

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

  • esri/map(第一个模块):Map(函数中的第一个变量)
  • esri/layers/FeatureLayer(第二个模块):FeatureLayer(第二个参数)
  • esri/dijit/Legend(第三个模块):Legend(第三个参数)
  • esri/dijit/HomeButton(第四个模块):HomeButton(第四个参数)
  • dojo/_base/array(第 5 个模块):arrayUtils(第 5 个参数)
  • dojo/parser(第 6 个模块):parser(第 6 个参数)
  • 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)

如果你把它们分开,你的代码之所以能工作,是因为你可能修复了你的 require().dojo.parser.parse() 之所以起作用,是因为它是不推荐使用的旧代码,不使用 require 回调的参数.

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.

这篇关于dojo 解析器和使用 ArcGIS JS API 的 TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆