Vue-cli 3.0 + cesium 构建

1. 环境介绍

changelog: [2018-11-12] 添加示例工程 ,添加导航插件

项目环境 版本
nodejs v9.2.0
npm 5.5.1
vue 2.5.17
vue-cli 3.0.0-rc.10
Cesium 1.48.0
2.环境安装
  1. nodejs 安装, 百度一下
  2. vue-cli 安装
~$ npm i -g @vue/cli
   #查看vue版本
~$ vue -V 
   3.0.0-rc.10
3.创建项目
  1. 使用vue-cli 创建项目
~$ vue create  你的项目名称   
  #接下来会让你选一下配置,这个不说了,百度一下大把
  1. 安装 cesium 依赖
~$ npm i cesium --save
  1. 新建 vue.config.js 文件,配置内容如下:
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 文件,内如如下

<template>
    <div id="cesiumContainer">

    </div>
</template>

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

<style scoped>

</style>

修改 router.js ,引入 CesiumViewer组件 , 内容如下

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 ,内容如下:

<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 内容如下

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 看效果吧。

推荐阅读更多精彩内容