top-tab.vue 2.13 KB
<template>
  <div>
    <van-tabs v-model:active="active" @click-tab="onTabClick">
      <van-tab
        v-for="(item, index) in tabRoutes"
        :title="item.title"
        :key="index"
        :name="item.name"
      >
        <slot :name="item.name"></slot>
      </van-tab>
    </van-tabs>
    <div v-show="active === 'share'">
      <share ref="shareRefs" :isShowOverlay="true">
        <div id="shareContainer" class="map-container"></div>
      </share>
    </div>
  </div>
</template>
<script>
import { defineComponent, nextTick, ref } from "vue";
import routes from "@/router/routes";
import { useRoute } from "vue-router";
import AMapLoader from "@amap/amap-jsapi-loader";
import { CONSTANT } from "../constant";
export default defineComponent({
  name: "TopTab",
  setup() {
    const tabRoutes = ref([]);
    const active = ref(null);
    const route = useRoute();
    let routeQuery = route.query;
    let map = null;
    let checkLoadedTimeoutId = "";
    tabRoutes.value = routes.tabRoutes;
    active.value = routeQuery.tabItem;
    const onTabClick = (e) => {
      if (e.name === "share") {
        nextTick(() => {
          console.log(active.value);
          AMapLoader.load({
            key: CONSTANT.MAP_KEY,
            version: "1.4.8",
            plugins: CONSTANT.MAP_PLUGINS,
          }).then((AMap) => {
            window.AMap = AMap;
            if (map) return;
            initMap("shareContainer");
          });
        });
      }
    };
    const initMap = (id) => {
      let check = () => {
        checkLoadedTimeoutId && window.clearTimeout(checkLoadedTimeoutId);
        let AMap = window.AMap;
        if (AMap && typeof AMap.Map == "function") {
          window.AMap = AMap;
          init(id);
        } else {
          checkLoadedTimeoutId = check();
        }
      };
      checkLoadedTimeoutId = check();
    };
    const init = (id) => {
      console.log(id);
      map = new window.AMap.Map(id);
      console.log(map);
      return map;
    };
    return {
      tabRoutes,
      active,
      onTabClick,
    };
  },
});
</script>
<style lang="scss">
.van-tabs__wrap {
  border-bottom: 1px solid #f0f2f5;
}
</style>