javascript - 为什么 openlayers 地图在 vue-cli 3 中不起作用
问题描述
我已经通过添加 ol 包到我的 vue-cli 项目中
npm 安装
但地图不加载。没有错误,我只是在结果源中找到了一个空的 div。
这是我的代码=>
html部分:
<div id="map-container"></div>
js部分:
import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZSource from 'ol/source/XYZ';
export default {
name: "page",
data() {
return {
...
}
},
methods: {
initMap: function () {
new Map({
target: 'map-container',
view: new View({
center: [0, 0],
zoom: 2
})
});
},
mounted: function () {
this.initMap();
},
}
注意=> 在某些地方我发现我必须将 init 函数调用为:
this.$nextTick(function () {
initMap();
})
但这没有任何区别。
伙计们,我的时间不多了,所以请帮助我。感谢所有想要帮助的人
解决方案
看起来您缺少 TileLayer。像这样的东西:
new Map({
target: "map-container",
layers: [
new TileLayer({
source: new XYZ({
url: "https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png"
})
})
],
view: new View({
center: [0, 0],
zoom: 2
})
});
推荐阅读
- c++ - How could I assign values to c++ vector
- java - SpringBoot JPA Repository - 获取所选字段显示错误“找不到列”
- python - 我试图将 pdf 转换为图像,但我不断收到这个错误 wand.exceptions.DelegateError
- java - 如何计算购物车 JList 中的价格总和?
- python - 如何从配置文件中获取数据
- java - 如何验证接受整数或带有 .5 的值的双精度值(Java)
- c# - Why is IsCancellationRequested not set to true on stopping a BackgroundService in .NET Core 3.1?
- java - 无法使用 JPA 创建映射实体
- html - html 表单标记中的操作属性未在 django 中发送数据
- angular - Angular formio 自定义组件