简体   繁体   English

如何将非模块(!)JavaScript 导入 Polymer 3.0

[英]How to import non-module(!) JavaScript into Polymer 3.0

Polymer 3 uses import to load external Javascript. Polymer 3 使用 import 加载外部 Javascript。 For example例如

import {GoogleCharts} from 'google-charts';

However, it seems for this to work, the external library should use exports.但是,这似乎可行,外部库应该使用导出。

I am trying to use mapbox-gl.js library.我正在尝试使用 mapbox-gl.js 库。 This library, installed with:这个库,安装了:

npm install mapbox-gl

does not seem to export anything.似乎没有出口任何东西。

In HTML5, you can use mapbox-gl as follows:在 HTML5 中,您可以使用 mapbox-gl 如下:

<script src="node_modules/mapbox-gl/dist/mapbox-gl.js"></script>
<link href="node_modules/mapbox-gl/dist/mapbox-gl.css" rel="stylesheet" />
<script>
   const options = {...}
   const map = new mapboxgl.Map(options);
</script>

I tried to use 'import' to load mapbox-gl:我尝试使用“导入”来加载 mapbox-gl:

import {mapboxgl} from './node_modules/mapbox-gl/dist/mapbox-gl.js';
import mapboxgl from './node_modules/mapbox-gl/dist/mapbox-gl.js';

This doesn't work:这不起作用:

Uncaught SyntaxError: The requested module './node_modules/mapbox-gl/dist/mapbox-gl.js' does not provide an export named 'mapboxgl'
Uncaught SyntaxError: The requested module './node_modules/mapbox-gl/dist/mapbox-gl.js' does not provide an export named 'default'

So, then I tried to add the script and css to document.head from inside the module javascript ( <script type="module">..</script> ):所以,然后我尝试从模块javascript ( <script type="module">..</script> ) 内部将脚本和 css 添加到 document.head :

// load external mapbox-gl.js script
const mapboxgljs = document.createElement('script');
mapboxgljs.setAttribute('src', 'node_modules/mapbox-gl/dist/mapbox-gl.js');
document.head.appendChild(mapboxgljs);
// load external mapbox-gl.css 
const mapboxcss = document.createElement('link');
mapboxcss.setAttribute('href', 'node_modules/mapbox-gl/dist/mapbox-gl.css');
mapboxcss.setAttribute('rel', 'stylesheet');
document.head.appendChild(mapboxcss);

This does not seem to work either.这似乎也不起作用。 If I try to use mapbox as follows:如果我尝试使用 mapbox 如下:

const map = new mapboxgl.Map(options)

I am getting:我正进入(状态:

Uncaught ReferenceError: mapboxgl is not defined

Edit:编辑:

Commenters @Salketer and @barbsan showed correct import syntax for this kind of library:评论者 @Salketer 和 @barbsan 显示了这种库的正确导入语法:

import 'node_modules/mapbox-gl/dist/mapbox-gl.js';

or或者

import * as mapboxgl from 'node_modules/mapbox-gl/dist/mapbox-gl.js';

Both now result in the following error message:现在两者都会导致以下错误消息:

Uncaught TypeError: Cannot set property 'mapboxgl' of undefined

This means the mapbox-gl library now gets loaded and interpreted.这意味着 mapbox-gl 库现在被加载和解释。 However, the mapbox-gl code contains the line:但是,mapbox-gl 代码包含以下行:

window.mapboxgl = something;

ES6-module scope does not have access to the browser 'window' object, so the code fails. ES6 模块范围无权访问浏览器的“窗口”对象,因此代码失败。 See also Is there an es6 module-scope equivalent to `window`?另请参阅是否有等效于 `window` 的 es6 模块范围? . .

For now, I am adding the HTML5 <script></script> and <link /> tags (see above) to the index.html of my project.现在,我将 HTML5 <script></script> 和 <link /> 标签(见上文)添加到我的项目的 index.html 中。 This works, but the idea of components and modules is to load dependencies from inside those components and modules?这是可行的,但是组件和模块的想法是从这些组件和模块内部加载依赖项?

You are close.你很近。

With your original code to load script:使用您的原始代码加载脚本:

// load external mapbox-gl.js script
const mapboxgljs = document.createElement('script');
mapboxgljs.setAttribute('src', 'node_modules/mapbox-gl/dist/mapbox-gl.js');
document.head.appendChild(mapboxgljs);

At this moment, if you use the instance provided by the script: const map = new mapboxgl.Map(options)此时,如果使用脚本提供的实例: const map = new mapboxgl.Map(options)

You will get error: Uncaught ReferenceError: mapboxgl is not defined你会得到错误: Uncaught ReferenceError: mapboxgl is not defined

Reason: the script isn't loaded yet, so the instance window.mapboxgl is undefined.原因:脚本尚未加载,因此实例window.mapboxgl未定义。

Solution: You have to wait until the script finished loading, using event listener.解决方案:您必须等到脚本完成加载,使用事件侦听器。 Add this code into your loading script code:将此代码添加到您的加载脚本代码中:

mapboxgljs.addEventListener('load', function() {
  // mapboxgl is available here, do whatever you want
  const map = new mapboxgl.Map(options)
})

对我来说,解决方案是定义没有 mustasch {} 的导入:

import mapboxgl from 'mapboxgl'

Just do:做就是了:

import("node_modules/mapbox-gl/dist/mapbox-gl");

// do my stuff with global dependency

or if import is slower than the code to be execute或者如果导入比要执行的代码慢

import("node_modules/mapbox-gl/dist/mapbox-gl").then(() => {
    // do my stuff with global dependency
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM