Vue-cli 3集成Cesium

之前记录了在Vue-Cli 2的基础上集成了Cesium,但现在Vue-Cli已更新到3,之前方法已不适用。在参考Vue-cli 3.0 + cesium 构建的基础上,验证记录Vue-Cli 3脚手架工具中集成Cesium的过程。

安装环境

  • node.js: v10.16.0
  • npm: 6.9.0
  • vue: 2.6.10
  • cesium: 1.45.0
  • vue-cli: 3.9.2
  • webpack: 4.28.4

安装配置

  • 安装nodejs

  • 安装vue-cli脚手架

1
2
3
4
npm install -g @vue/cli
vue create my-project
cd my-project
npm run serve
  • 安装 cesium
1
npm i cesium --save
  • 新建 vue.config.js 文件,配置内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const CopyWebpackPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const path = require('path')

const debug = process.env.NODE_ENV !== 'production'
let cesiumSource = './node_modules/cesium/Source'
let cesiumWorkers = '../Build/Cesium/Workers'
module.exports = {
baseUrl: '',
devServer: {
port: 9999
},
configureWebpack: {
output: {
sourcePrefix: ' '
},
amd: {
toUrlUndefined: true
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': path.resolve('src'),
'cesium': path.resolve(__dirname, cesiumSource)
}
},
plugins: [
new CopyWebpackPlugin([ { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers'}]),
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Assets'), to: 'Assets'}]),
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets'}]),
new CopyWebpackPlugin([ { from: path.join(cesiumSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers'}]),
new webpack.DefinePlugin({
CESIUM_BASE_URL: JSON.stringify('./')
})
],
module: {
unknownContextCritical: /^.\/.*$/,
unknownContextCritical: false

}
}
}
  • 在 src/components 下新建 CesiumViewer.vue 文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<template>
<div id="cesiumContainer" class="fullSize">

</div>
</template>

<script>
export default {
name: "CesiumViewer"
}
</script>

<style scoped>
.fullSize {
display: block;
position: absolute;
top: 0;
left: 0;
border: none;
width: 100%;
height: 100%;
}
</style>
  • 修改 router.js ,引入 CesiumViewer组件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import Vue from 'vue'
import Router from 'vue-router'
import CesiumViewer from './components/CesiumViewer.vue'

Vue.use(Router)

export default new Router({
routes: [
{
path: '/',
name: 'CesiumViewer',
component: CesiumViewer
}
]
})
  • 修改App.vue ,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<template>
<div id="app">
<router-view/>
</div>
</template>

<style lang="css">
html, body, #cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
</style>
  • 最后修改 main.js 内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Cesium from 'cesium/Cesium'
// noinspection ES6UnusedImports
import widget from 'cesium/Widgets/widgets.css'

Vue.config.productionTip = false

new Vue({
router,
store,
render: h => h(App),
mounted () {
var viewer = new Cesium.Viewer('cesiumContainer')
}
}).$mount('#app')
  • 最后 npm serve ,打开浏览器输入localhost:8080查看效果。

注意事项

不应将viewer定义在vue组件的data对象中,因为vue会劫持数据,导致cesium卡顿。具体请参考vue+cesium cesium数据量太大导致浏览器卡顿解决办法

参考链接

  1. Vue-cli 3.0 + cesium 构建,by QingMings.
  2. cesium-and-webpack,by marsgis.
  3. Vue.js集成Cesium,by jack huang.
  4. Cesium笔记 — Vue+Cesium开发环境搭建(基于Cesium1.69),by 芒果香蕉.
  5. vue+cesium cesium数据量太大导致浏览器卡顿解决办法,by 浮年似水.