基于Webpack的Cesium+Vue2.x應(yīng)用(推薦)

本文主要講解vue2.x與cesium的結(jié)合,主要參考cesium官網(wǎng)教程(Cesium and Webpack)和github下的cesium-vue項(xiàng)目。

1、使用vue-cli腳手架新建vue項(xiàng)目(vue init webpack 項(xiàng)目名稱),引入cesium模塊(cnpm install cesium)

image.png

2、修改webpack下的3個(gè)配置文件:webpack.base.conf.js、webpack.dev.conf.js、webpack.prod.conf.js。具體如下:

1、修改webpack.base.conf.js文件

(1)增加代碼


image.png
const cesiumSource = '../node_modules/cesium/Source';       

(2)增加代碼


image.png
    sourcePrefix: ''

    amd: {                                    //flag
        // Enable webpack-friendly use of require in Cesium
        toUrlUndefined: true
    },

(3)增加代碼


image.png
'cesium': path.resolve(__dirname, cesiumSource)

(4)增加代碼


image.png
{
            //Strip cesium pragmas 刪除編譯指示               //flag
            test: /\.js$/,
            enforce: 'pre',
            include: path.resolve(__dirname, cesiumSource),
            use: [{
                loader: 'strip-pragma-loader',
                options: {
                    pragmas: {
                        debug: false
                    }
                }
            }]
        },

(5)增加代碼


image.png
unknownContextCritical: false  //屏蔽警告
2、修改webpack.dev.conf.js文件

(1)增加代碼


image.png
const cesiumSource = 'node_modules/cesium/Source';      //flag
const cesiumWorkers = '../Build/Cesium/Workers';

(2)增加代碼


image.png
'CESIUM_BASE_URL': JSON.stringify('') 

(3)增加代碼


image.png
      new CopyWebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' } ]),     //flag
      new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets' } ]),
      new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' } ]),
3、修改webpack.prod.conf.js文件

(1)增加代碼


image.png
const cesiumSource = 'node_modules/cesium/Source';      //flag
const cesiumWorkers = '../Build/Cesium/Workers';

(2)增加代碼


image.png
'CESIUM_BASE_URL': JSON.stringify('static')

(3)增加代碼


image.png
      new CopyWebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'static/Workers' } ]),//flag
      new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'static/Assets' } ]),
      new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'static/Widgets' } ]),

3、測試

cesiumViewer.vue代碼:

<template>
    <div id="cesiumContainer">
    </div>
</template>

<script>
    import Cesium from 'cesium/Cesium';
    import widgets from 'cesium/Widgets/widgets.css';

    export default {
        name: 'cesiumContainer',
        mounted(){
            var viewer = new Cesium.Viewer('cesiumContainer');
        }
    }
</script>

<style scoped>
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容