Commit f7ee02a5 authored by aoobao's avatar aoobao

add map init plugin

parent 5d4ca1d6
<script>
import register from '@/components/map/Register'
// import { mapGetters } from 'vuex'
// import { getDateString } from '@/assets/js/utils'
// import AMapUtils from 'amap-utils'
export default {
mixins: [register],
methods: {
// 组件入口 不需要写在create中
init () {
},
// 销毁组件调用.
destroy () {
}
}
}
</script>
<template> <template>
<div class="map" ref="map"> <div class="map" ref="map">
<slot></slot> <slot v-if="isCreated"></slot>
</div> </div>
</template> </template>
<script> <script>
...@@ -13,7 +13,7 @@ export default { ...@@ -13,7 +13,7 @@ export default {
default () { default () {
return { return {
zoom: 8,//级别 zoom: 8,//级别
viewMode: '2D'//使用3D视图 viewMode: '2D'
} }
} }
} }
...@@ -21,7 +21,8 @@ export default { ...@@ -21,7 +21,8 @@ export default {
data () { data () {
let amap = {} let amap = {}
return { return {
amap amap,
isCreated: false
} }
}, },
provide () { provide () {
...@@ -48,11 +49,20 @@ export default { ...@@ -48,11 +49,20 @@ export default {
...this.opts ...this.opts
}) })
setTimeout(() => { setTimeout(() => {
this.$emit('created') this.isCreated = true
this.$emit('created', this.getMap())
}, 1); }, 1);
}, },
getMap () { getMap () {
return this.amap.$map || null return this.amap.$map || null
},
applyMethod (methodName, ...params) {
let map = this.getMap()
if (!map) {
console.warn('not ready')
return
}
return map[methodName](...params)
} }
} }
} }
......
<script>
// 高德地图注册事件(所有地图组件mixins)
// 所有地图组件实现init 方法,初始化结束后会自动调用init
// 组件销毁时会自动调用destroy方法,
// 如果实例下绑定有$object(this.$object),会在地图中移除
export default {
render () { return null },
inject: ['amap'],
created () {
let map = this.getMap()
typeof this.init === 'function' && this.init(map)
},
beforeDestroy () {
typeof this.destroy === 'function' && this.destroy()
this.destroyObject()
},
methods: {
// 通用方法 TODO
getObject () {
return this.$object || null
},
setObject (object) {
this.destroyObject()
this.$object = object
},
getMap () {
return this.amap.$map || null
},
destroyObject () {
if (this.$object) {
if (typeof this.$object === 'object') {
if (typeof this.$object.setMap === 'function') {
this.$object.setMap(null)
}
if (typeof this.$object.destroy === 'function') {
this.$object.destroy()
}
this.$object = null
} else {
console.warn('$object is not object')
}
}
}
}
}
</script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment