Official documentation: OpenLayers - Welcome
What I came into contact with is that the map is used in the project after downloading it using npm as a plug-in.
1. In the map, first encapsulate and expose a method to mount on Vue
import WhrdMap from './whrd-map/WhrdMap.vue'
import { optConfig } from './whrd-map/option.config'
WhrdMap.install = function (Vue, opts = {}) {
optConfig.request = opts.request || window.axios
Vue.component(WhrdMap.name, WhrdMap)
Vue.prototype.$mapEventHub = Vue.prototype.$mapEventHub || new Vue()
}
export default WhrdMap
2. After installing the plug-in in the project, trigger the method exposed by the map in the used file, and the value that needs to be assigned
this.$mapEventHub.$emit("setzoom-popup",15)
3. Subscribe on the map page
this.doSubscribe("setzoom-popup", this.setZoomAndonEvent);
4. Define the method trigger of the subscription
doSubscribe(message, callback) {
// 订阅
this.$mapEventHub.$on(message, callback);
// 取消订阅 - 回收处理
this.$once("hook:beforeDestroy", () => {
this.$mapEventHub.$off(message, callback);
});
},
5. Define a method Here is the built-in method of ol to operate on the map
setZoomAndonEvent(zoom) {
this.showZoom = zoom;
let map = this.getMap();
let mapView = (map && map.getView()) || null;
if (mapView) {
mapView.on("change:resolution", this.resolutionChange);
}
},