【发布时间】:2018-09-11 21:11:03
【问题描述】:
我正在尝试使用 vue-cli-3 实现 Openlayers,但似乎我做错了,我错过了一些东西。
首先,我安装了 vue cli
npm install @vue/cli -g
然后我添加了额外的依赖,或者更准确地说是 OpenLayers 库。
npm install ol.
但我在全局注册 ol 时添加/注册依赖项(在 main.js 文件中)以某种方式失败
当我导入这样的文件时,在我的 App.vue 文件中它不起作用。我收到这两个错误
[Vue 警告]:nextTick 中的错误:“ReferenceError: ol is not defined”
ReferenceError: ol 未定义
import 'ol/ol.css';
import { Map, View } from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
export default {
data() {
return {
testing: 'SomeTests',
}
},
mounted() {
this.$nextTick(function () {
initMap();
})
}
}
function initMap() {
var myMap = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2,
})
})
};
但是当我在 index.html 中包含脚本和链接标记时,上面的代码可以正常工作。
<head>
<link rel="stylesheet" href="https://openlayers.org/en/v5.2.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.2.0/build/ol.js"></script>
<title>ol-basic</title>
</head>
我的问题是这样的。是否可以像 ol page 上推荐的那样使用模块导入元素,是否可以在我的 main.js 文件中以某种方式全局注册 ol 包
【问题讨论】:
标签: javascript vue.js openlayers vue-cli