reactjs - 在 webpack 应用程序中引用 amd 模块(arcgis)

标签 reactjs webpack arcgis arcgis-js-api

我正在使用 webpack 构建一个 React 应用程序,我需要合并 arcgis maps进入 react 组件。我知道如何将其带入我的项目中。我尝试使用内置 JavaScript 的 index.js 创建一个 arcgis 目录,并尝试引用该目录:

import {Map} from 'arcgis/index'

那是行不通的。然后,我尝试将 css/js 脚本标签直接包含到我的 index.html 中,但是当我尝试 require 它们时,就像示例中那样,webpack 显然不能找到他们。有没有办法告诉 webpack 忽略 src 文件中的 require 调用,以便浏览器处理它?我正在尝试执行以下操作但失败了:

import React from 'react'

export default class EsriMap extends React.Component {
    componentDidMount() {
        const _this = this

        require(["esri/map", "dojo/domReady!"], function(Map) {
          var map = new Map(_this.refs.map, {
            center: [-118, 34.5],
            zoom: 8,
            basemap: "topo"
          });
        });
    }

    render() {
        return (
            <div ref="map"></div>
        )
    }
}

最佳答案

您可能想尝试这个 https://github.com/tomwayson/esri-webpack-babel .

这个方法很好,因为它不会使构建陷入困境。您从 CDN 中提取 ESRI Api,并告诉 webpack 它是外部的。

//Add this...
externals: [
 // Excludes any esri or dojo modules from the bundle.
 // These are included in the ArcGIS API for JavaScript, 
 // and its Dojo loader will pull them from its own build output
  function (context, request, callback) {
    if (/^dojo/.test(request) ||
        /^dojox/.test(request) ||
        /^dijit/.test(request) ||
        /^esri/.test(request)
    ) {
      return callback(null, "amd " + request);
    }
    callback();
  }
],
//And this to you output config
output: {
  libraryTarget: "amd"
},

当您的应用程序加载时,您可以在脚本标记中使用 Dojo 引导 Webpack 模块。

<!-- 1. Configure and load ESRI libraries -->
<script>
    window.dojoConfig = {
        async: true
    };
</script>
<script src="https://js.arcgis.com/4.1/"></script>

<!-- Load webpack bundles-->
<script>
    require(["Angular/dist/polyfills.bundle.js", "Angular/dist/vendor.bundle.js", "Angular/dist/app.bundle.js"], function (polyfills, vendor, main) { });
</script>

我已经让它与我正在开发的 Angular 2 应用程序一起工作。唯一的缺点是我还没有使用 Karma 正确运行单元测试。我现在只花了几个小时。希望很快就能解决测试问题。

关于reactjs - 在 webpack 应用程序中引用 amd 模块(arcgis),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36919600/

相关文章:

javascript - browser-sync-webpack-plugin 不工作

python - Jupyter 实验室无法安装任何扩展 : Assertion `data' failed

javascript - NPM 模块导致 Web 应用程序无法在 IE11 上呈现

javascript - 使用 ArcGIS JS API 在浏览器中显示 ArcGIS 形状文件 (*.shp)

javascript - 重置 useState 的初始状态

javascript - 为什么对话框模式需要放置为主体的最后一个直接子级?[辅助功能,a11y]

c# - 将搜索和排序功能添加到数据绑定(bind)包装器

python - 为什么我的 python while 循环不会停止?

javascript - 当我从 Typescript 中的 NPM 包导入类型时,它来自哪个文件?

reactjs - 将内联 SVG 作为 React 组件 prop 传递