我尝试使用openlayers添加一个geojson图层。
但在vue2.x版本中,geojson文件无法导入,因此无法添加图层。
我现在的webpack配置是(vue.config.js):
”
const { VueLoaderPlugin } = require('vue-loader');
const path = require('path');
module.exports = {
lineOnSave: false,
devServer: {
proxy: 'http://localhost:8080'
},
configureWebpack: {
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.geojson$/,
loader: 'json-loader'
}
],
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx", 'json', 'geojson'],
},
plugins : [
new VueLoaderPlugin(),
],
}
}
}
”
vue文件中mainMap.vue的代码是:
”
import koreaPath from '../assets/geojsonFile/korean.geojson'
mounted(){
this.createKoreaMap()
}
data() {
return{
olmap: undefined,
}
},
methods: {
createKoreaMap() {
this.olmap = new OlMap({
target: this.$refs.map,
layers: [
new OlLayerTile({
source: new OSM({
url: 'https://xdworld.vworld.kr/2d/midnight/service/{z}/{x}/{y}.png'
})
})
],
view: new OlView({
center: fromLonLat([126.978275264, 37.566642192]), // 경기도 성남
zoom: 10
})
})
}
”
最后,当我使用npm run serve构建时,错误是:
@ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/components/mainMap.vue?vue&type=script&las!./src/components/mainMap.vue?vue&type=script&lang=js& 14:0-61
@ ./src/components/mainMap.vue?vue&type=script&lang=js& 1:0-201 1:217-220 1:222-420 1:222-420
@ ./src/components/mainMap.vue 2:0-59 3:0-54 3:0-54 10:2-8
@ ./src/components/mainMap.vue?vue&type=script&lang=js& 1:0-201 1:217-220 1:222-420 1:222-420 s!./src/App.vue?vue&type=script&lang=js& 1:0-47 9
@ ./src/components/mainMap.vue 2:0-59 3:0-54 3:0-54 10:2-8
@ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/App.vue?vue&type=script&lang=js& 1:0-47 9:4-11
@ ./src/App.vue?vue&type=script&lang=js& 1:0-191 1:207-210 1:212-400 1:212-400
@ ./src/App.vue 2:0-55 3:0-50 3:0-50 10:2-8
@ ./src/main.js 2:0-28 7:17-20
webpack compiled with 1 error
我试过不同的方法,但我已经痛苦了两天。我该怎么办??
1条答案
按热度按时间ljo96ir51#
下面是如何在Vue中导入JSON文件的示例:
下面是一个webpack(v5)配置的例子:
我希望这对你有帮助!