【问题标题】:Package esri is not found找不到包 esri
【发布时间】:2023-03-21 06:10:01
【问题描述】:

我在带有打字稿的 AngularJs 2 中使用 esri

import esri = require('esri');
import Map = require('esri/map');
import AGSPoint = require("esri/geometry/Point");

import { Component } from 'angular2/core'

@Component({
  templateUrl: 'app/home/home.html',
  styleUrls: [ 'app/home/home.css' ],
})
export class Home {
  map: Map;

  contructor() {
    this.map = this.createMap();
  }

  private createMap(): Map {
    const point = new Point(-122.45, 37.75); // long, lat
    point.log();

    const mapOptions: esri.MapOptions = {};
    mapOptions.basemap = "topo";
    mapOptions.center = point;
    mapOptions.zoom = 13;

    return new Map("map", mapOptions);
  }
}

class Point extends AGSPoint {

  log() {
    console.log(this.type, this.x, this.y);
  }
}

我已经正确安装了类型定义(d.ts 文件)并且它们被正确检索。

索引文件如下所示

<html>

  <head>
    <base href="/">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
    <title>Home</title>

    <link rel="stylesheet" href="https://js.arcgis.com/3.15/esri/css/esri.css">

    <!-- IE required polyfills, in this exact order -->
    <script src="es6-shim/es6-shim.min.js"></script>
    <script src="systemjs/dist/system-polyfills.js"></script>

    <script src="angular2/bundles/angular2-polyfills.js"></script>
    <script src="systemjs/dist/system.src.js"></script>
    <script src="rxjs/bundles/Rx.js"></script>
    <script src="angular2/bundles/angular2.dev.js"></script>
    <script src="angular2/bundles/router.dev.js"></script>

    <!-- ArcGIS -->
    <script src="//js.arcgis.com/3.15"></script>
    <script>
      System.config({
        // defaultJSExtensions: true,
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/main')
        .then(null, console.error.bind(console));
    </script>

  </head>

  <body>
    <app>Loading map...</app>
  </body>

</html>

关键是当我执行应用程序时它不起作用。 index.html 由 nodejs 提供。它给了我错误

localhost:3000/esri/map not found

我应该如何安装esri modules?应该如何检索它们?

【问题讨论】:

  • 脚本在哪里?
  • 你试过了吗:import esri = require('./esri');
  • 我已经导入了 td.s 定义。 Esri 脚本不存在。应该以某种方式检索它们,但从哪里检索?
  • 它们是 npm 模块吗?

标签: angularjs node.js typescript angular esri


【解决方案1】:

为了正确检索模块,我们需要使用 SystemJS 指令 mapesri 映射到包的位置

System.config({
        defaultJSExtensions: true,
        map: {
          esri: '//js.arcgis.com/3.15/esri'
        },
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });

现在包已正确检索,但仍然出现另一个错误,提示 define 不起作用

【讨论】:

    【解决方案2】:

    欢迎来到 Angular2 和 TypeScript 中令人困惑的第 3 方模块的文档世界!根据您的设置和环境,这里有很多您可能需要的拼图:

    //custom_typings.d.ts

    declare module 'my-module-name' {
      export function foo() : any;
    }
    

    //app.component.ts

    import * as MyModule from 'my-module-name';
    

    //index.html

    System.config({
        defaultJSExtensions: true,
        packages: {
            app: {format: 'register', defaultExtension: 'js'},
            "/angular2": {"defaultExtension": false}
        },
        map: {
            'my-module-name': 'node_modules/my-module-name'
        }
    });
    

    //如果你有一个已注册的 npm 模块,那么你可以直接 cli 安装它:

    `npm install my-module-name --save`
    

    //或者,如果不是,那么您可以在package.json 中放置自定义安装绑定:

     "dependencies": {
        "angular2": "2.0.0-beta.0",
        "bootstrap": "^3.3.6",
        "es6-promise": "^3.0.2",
        "es6-shim": "^0.33.3",
        "my-module-name": "git+https://stash.somecompany.com/my-module.name.git#0.1.0"
    }
    

    【讨论】:

    • 好的,我明白了,谢谢你的解释。顺便说一句,现在一切都清楚了,我们分析了问题并提出了我需要的解决方案,以某种方式使用 dojo 加载esri。关键是如何将 dojo 与 systemjs 一起使用?
    • Dojo 使用 AMD 模块,SystemJS 可以通用加载 AMD 模块。
    • 这是个好消息,但对我来说仍然是个谜:-)
    • 我需要使用dojo加载esri,但是systemjs如何使用dojo。我没有看到任何示例
    • 如果我们尝试按照我发布的示例进行操作,那么我应该使用 System.import 加载 dojo,但不幸的是它不起作用。我收到localhost:3000/dojo/domReady 未找到。也许这是服务器的一些错误配置。我的意思是 index.html 由 nodejs 服务器提供服务,我在服务器中设置了字符串 app.use(express.static(path.join(__dirname, '../client'))) 以获取静态内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2019-11-11
    • 2020-12-17
    • 2013-07-25
    • 2014-02-27
    • 2023-03-06
    • 2015-04-06
    相关资源
    最近更新 更多