GIS

Vue + Cesium环境部署

2020-03-20  本文已影响0人  仙人掌开不了花

一、基本环境安装

  1. node.js
  2. webpack安装
  3. vue -cli 安装

二、vue项目搭建

1.初始化项目

vue init webpack project_name

2.安装相关的工具

cd project_name
npm install

3.启动

npm run dev

三、Cesium环境配置

1.安装Cesium

npm install cesium --save

2.在build/webpack.base.conf.js下

①定义cesium源码路径

const cesiumSource = '../node_modules/cesium/Source'

②在module.exports中,output添加sourcePrefix:' ',让webpack正确处理多行字符串

output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath,
    sourcePrefix: ' '
  },

③在output后面,添加amd模式支持

output: {...},
amd: {
  toUrlUndefined: true
},

④在resolve中设置cesium别名,引入的时候直接根据别名就可以找到cesium包了

 resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
      'cesium': path.resolve(__dirname, cesiumSource)
    }
  },

⑤module里添加unknownContextCritical:false,让webpack打印载入特定库时候告警

module: {
   rules: [...],
   unknownContextCritical: false,
   // unknownContextRegExp: /^.\/.*$/,   //这个很多都推荐了,但是实测的时候发现会报错,先不用
},

3.配置webpack.dev.conf.js

①配置路径

const cesiumSource = 'node_modules/cesium/Source'
const cesiumWorkers = '../Build/Cesium/Workers'

②在plugins下面添加如下插件

plugins: [
   ...,
    // Copy Cesium Assets, Widgets, and Workers to a static directory
    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({
       // Define relative base path in cesium for loading assets
       CESIUM_BASE_URL: JSON.stringify('')
    })
  ]

4.配置webpack.prod.conf.js文件

①配置路径

const cesiumSource = 'node_modules/cesium/Source'
const cesiumWorkers = '../Build/Cesium/Workers'

②在plugins下面添加如下插件,拷贝静态资源。与dev配置略有不同

plugins: [
   ...,
    // Copy Cesium Assets, Widgets, and Workers to a static directory
    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从哪里加载资源,如果使用默认的'',却变成了绝对路径了,所以这里使用'./',使用相对路径
       CESIUM_BASE_URL: JSON.stringify('./')
    })
  ]

5.修改config里index.js文件,build中的assetsPublicPath:"./"

assetsSubDirectory: 'static',
assetsPublicPath: '',

四、调用Cesium

有以下两种方式

1.全局配置

①配置main.js

import Cesium from 'cesium/Cesium'
import widgets from 'cesium/Widgets/widgets.css'
 
Vue.prototype.Cesium = Cesium
Vue.prototype.widgets = widgets

②直接在组件中调用

var Cesium = this.Cesium
Cesium...

2.组件中配置

import Cesium from 'cesium/Cesium'
import widgets from 'cesium/Widgets/widgets.css'
<script>
export default {
  data () {...},
  mounted(){
    Cesium...
  }
}
</script>

3.在1.60版本以上的Ceisum配置

const Cesium = require('cesium/Cesium')
import widgets from 'cesium/Widgets/widgets.css';
<script>
export default {
  data () {...},
  mounted(){
    Cesium...
  }
}
</script>

五、可能遇到的问题

1.运行时报如下错误

"export 'default' (imported as 'Cesium') was not found in 'cesium/Cesium'

Cesium在1.6版本以后只能通过require导入了,用import就会报如上错误,具体导入代码,请参考四-3

参考文档

1.vue 2.x+webpack+cesium1.64(1)详解:环境部署、项目搭建与配置
2.Vue+Cesium项目环境搭建

上一篇下一篇

猜你喜欢

热点阅读