1、创建一个js文件
export function BMPGL(ak) { return new Promise(function (resolve, reject) { window.init = function () { // eslint-disable-next-line resolve(BMapGL) } const script = document.createElement('script') script.type = 'text/javascript' script.src = `http://api.map.baidu.com/api?v=3.0&type=webgl&ak=${ak}&callback=init` script.onerror = reject document.head.appendChild(script) }) }
2、在组件中使用
const init = () => { BMPGL('ak值').then( (BMapGL) => { let map = new BMapGL.Map('container') // 创建点坐标 axios => res 获取的初始化定位坐标 let point = new BMapGL.Point(116.404, 39.915) // 初始化地图,设置中心点坐标和地图级别 map.centerAndZoom(point, 15); } ) }