Skip to content

Commit

Permalink
feat: fix 自定义组件不显示和现实位置不对
Browse files Browse the repository at this point in the history
  • Loading branch information
yue1123 committed Apr 20, 2022
1 parent 6d6ef6e commit c0d7e6e
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/components/control/bm-control/index.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<div ref="controlContainer" v-show="show">
<div ref="controlContainer">
<slot></slot>
</div>
</template>

<script setup lang="ts">
import { ref, defineProps, withDefaults } from 'vue'
import { ref, defineProps, withDefaults, nextTick, onMounted } from 'vue'
import useLife from '../../../hooks/useLife'
import useBaseMapEffect from '../../../hooks/useBaseMapEffect'
export interface baseBmControlOptions {
Expand All @@ -23,23 +23,25 @@
}
const controlContainer = ref<HTMLDivElement>()
const { ready } = useLife()
const show = ref<boolean>(false)
const props = withDefaults(defineProps<baseBmControlOptions>(), {
anchor: 'BMAP_ANCHOR_TOP_LEFT',
offset: () => ({ x: 83, y: 18 })
})
defineEmits(['initd', 'unload'])
useBaseMapEffect((map: BMapGL.Map) => {
if (!controlContainer.value) return
const customControl = new window.BMapGL.Control()
customControl.defaultAnchor = window[props.anchor]
customControl.defaultOffset = new window.BMapGL.Size(props.offset!.x, props.offset!.y)
customControl.initialize = (_map: BMapGL.Map) => {
show.value = true
return _map.getContainer().appendChild(controlContainer.value as Node) as HTMLElement
}
map.addControl(customControl)
ready(map)
return () => map.removeControl(customControl)
onMounted(() => {
useBaseMapEffect((map: BMapGL.Map) => {
const customControl = new window.BMapGL.Control()
if (!controlContainer.value) return
customControl.defaultAnchor = window[props.anchor]
customControl.defaultOffset = new window.BMapGL.Size(props.offset!.x, props.offset!.y)
customControl.initialize = (_map: BMapGL.Map) => {
return _map.getContainer().appendChild(controlContainer.value as Node) as HTMLElement
}
// nextTick(() => {
map.addControl(customControl)
ready(map)
// })
return () => map.removeControl(customControl)
})
})
</script>

0 comments on commit c0d7e6e

Please sign in to comment.