# 访问 AMap 实例对象

当你需要显式访问 AMap 的实例对象时,请按如下步骤

  1. 先通过$refs获取 AMapVue 对象
<template>
  <div>
    <amap ref="myMap">
      <amap-marker ref="myMarker" />
    </amap>
  </div>
</template>
1
2
3
4
5
6
7
this.$refs.myMap // 地图 AMapVue 对象
this.$refs.myMarker // 点标记 AMapVue 对象
1
2
  1. 再通过$map$target属性,访问 AMap 原生对象
this.$refs.myMap.$map // AMap.Map 原生对象
this.$refs.myMarker.$target // AMap.Marker 原生对象
1
2
  1. 使用时请注意

由于 AMap 和 AMapVue 部分功能是异步的,因此如果你在第一时间(如mounted())内访问this.$refs.xxx获取 AMapVue 对象时,可能为undefined

这种时候你需要适当地延后你的代码执行时机,例如使用$nextTick