对于一个项目,我必须使用RequireJS来加载mapbox库。但是,它不起作用,我总是收到以下错误消息:
未定义的ReferenceError: mapboxgl未定义
这是我的RequireJS代码:
requirejs.config({
{
...
'leaflet-mapbox-gl': 'js/leaflet-mapbox-gl',
'mapbox-gl': 'js/mapbox-gl'
}我想和传单-mapbox-gl一起用。没有RequireJS,一切都能工作,但显然不是我想要的。我还尝试了以下shim配置,但没有成功:
shim : {
'mapbox-gl': {
exports: ['mapboxgl']
}
'leaflet-mapbox-gl': {
deps: ['leaflet','mapbox-gl']
}
}如何使用RequireJS正确加载mapbox?
发布于 2017-08-07 20:54:34
我自己想出来的。问题不是mapbox,而是传单-mapbox。必须围绕传单-mapbox包装以下代码,以使其与RequireJS一起工作:
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module depending on mapboxgl.
define(['mapboxgl'], factory);
} else {
// No AMD.
factory(mapboxgl);
}
}(function (mapboxgl) {
//...original leaflet-mapbox-gl code
}));这是我的垫片配置:
shim :
{
// other code ...
'mapboxgl' : {
exports: 'mapboxgl'
},
'leaflet-mapbox-gl': {
deps: ['leaflet','mapboxgl']
}
}希望这能帮到别人
https://stackoverflow.com/questions/45542737
复制相似问题