node 異步加載百度地圖
阿新 • • 發佈:2018-12-14
cti return pro clas tgs func span ret nod
export default { init: function (){ const AK = "AFNGWoP3YTGstfhT6BQUcsm5xGodgTIv"; const BMap_URL = "https://api.map.baidu.com/api?v=3.0&ak="+ AK +"&s=1&callback=onBMapCallback"; return new Promise((resolve, reject) => { // 如果已加載直接返回 if(typeof BMap !== "undefined") { resolve(BMap); return true; } // 百度地圖異步加載回調處理 window.onBMapCallback = function () { // console.log("百度地圖腳本初始化成功..."); resolve(BMap); }; // 插入script腳本 let scriptNode = document.createElement("script"); scriptNode.setAttribute("type", "text/javascript"); scriptNode.setAttribute("src", BMap_URL); document.body.appendChild(scriptNode); }); } }
MP.init().then((BMap)=>{ this.init() });
node 異步加載百度地圖