准备工作
- 在public中的index.html中初始化地图
<script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
- 在 .eslintrc.js 中配置AMap和AMapUI
globals: {
"AMap": "true",
"AMapUI":"true",
}
实现思路
- 封装一个全局组件scottMap.vue,用于加载地图
- 封装一个基于scottMap.vue的组件mapVectorLine.vue,用于在地图上画出矢量图形
- 通过v-if和组件传参,确定需要的地图组件
- 组件嵌套封装代码(一个地图上面有多个功能),相比一个地图上封装一个功能,这种方式加载速度更快
scottMap.vue
<template>
<div class="m-map">
<div id="js-container" class="map">正在加载数据 ...</div>
<div class="route">
<map-vector-line v-if="isVectorLine" :map="map"></map-vector-line>
</div>
</div>
</template>
<script>
import MapVectorLine from './scottMap/mapVectorLine.vue'