简体   繁体   中英

importing javascript modules from a third party library

In my code I already use the mapbox-gl-js library to create a map by using this import:

let mapboxgl = require('mapbox-gl/dist/mapbox-gl.js')

which works fine.

Now I would like to use the 'create' function that exists within mapbox-gl.js in its dom module: this one to be more precise.

I was hoping I could simply use this:

mapboxgl.dom.create(arguments)

but then I get errormessage:

Uncaught (in promise) TypeError: Cannot read property 'create' of undefined

Can I use the 'create function' of the library and if so, how?

The file you are actually require -ing is the index.js file , which does not export the dom symbol, because this is an internal utility of mapbox. You can always copy the file dom.js and use it if you need to, but mapbox doesn't expose this file for you to use.

EDIT:

If you are determined to use the file, you can require it directly, although it is not advisable, since mapbox could decide to change or remove the function at any version upgrade. In my case I installed the package via npm, so I required it like so let mbDom = require('mapbox-gl/src/util/dom'); .

While this is possible, it is far from advisable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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