Home >Web Front-end >JS Tutorial >Share 2 ways to use Baidu Maps in Vue
Maps are used a lot in the project, and I have used them several times recently. I can’t remember them even after not using them for a long time. In my spare time, I compiled two methods of using Baidu Maps in Vue to facilitate my own viewing and share them with everyone. , hoping to help those in need.
The common method is:
1.Introduce
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>
2. Create a new component maps
Note : Do not name the component map, because if there is a map tag in HTML, an error will be reported
Error: Do not use built-in or reserved HTML elements as component id:map
3. Then You can directly re-assemble the components and write the relevant code
mounted(){ var map = new BMap.Map('map') var point = new BMap.Point(108.840053, 34.129522) map.centerAndZoom(point, 14) //... }
Another method:
1. Create a new map.js
export function MP(ak) { return new Promise(function (resolve, reject) { window.onload = function () { resolve(BMap) } var script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://api.map.baidu.com/api?v=2.0&ak="+ak+"&callback=init"; script.onerror = reject; document.head.appendChild(script); }) }
2. When needed Introduce
import {MP} from './map.js'
3 into the vue page of the map used. Call
data:{ return{ ak:'1287348913029483740293'//密钥 } }, mounted(){ this.$nextTick(function(){ var _this = this; MP(_this.ak).then(BMap => { //在此调用api }) } }in the vue page
The above is the detailed content of Share 2 ways to use Baidu Maps in Vue. For more information, please follow other related articles on the PHP Chinese website!