首页 > 解决方案 > Mapbox not works global 未定义

问题描述

我不知道为什么总是得到这个错误,尝试了很多次绕过它。也许谁知道解决方案并有这个错误?

应用程序模块

NgxMapboxGLModule.withConfig(
      {token added..
          }),

包裹

  "mapbox-gl": "^0.52.0",
    "ngx-mapbox-gl": "^3.0.1",
    "@types/mapbox-gl": "^0.51.4",
    "@mapbox/geojson-area": "^0.2.2",
    "@mapbox/mapbox-gl-geocoder": "^3.1.6",
    "@mapbox/simplespec-to-gl-style": "^0.3.2",


index.js:43 Uncaught ReferenceError: global is not defined
    at Object../node_modules/buffer/index.js (index.js:43)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/safe-buffer/index.js (index.js:2)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/sha.js/hash.js (hash.js:1)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/sha.js/sha.js (sha.js:10)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/sha.js/index.js (index.js:10)
    at __webpack_require__ (bootstrap:79)

标签: angularmapbox

解决方案


来自ngx-mapbox-gl README 的解决方案:

将此添加到您的 polyfill.ts 文件中(https://github.com/Wykks/ngx-mapbox-gl/issues/136#issuecomment-496224634):

(window as any).global = window;

ngx-mapbox-gl 依赖于 sha.js,它调用在浏览器中不可用的“全局”(它是一个 Node.js 变量)。上面的代码将 'global' 填充为 'window' (在浏览器中可用)。Angular 6 及更高版本为 Node.js 特定变量和库(加密、http 等)删除 webpack 的 polyfill 导致的更广泛问题的一部分

Angular 6 many Can't resolve errors (crypto, fs, http, https, net, path, stream, tls, zlib)

本文底部的相关部分


推荐阅读