小言_互联网的博客

vue4+Cesium1.81.0安装及配置

702人阅读  评论(0)

vue4+Cesium1.81.0

一、基本环境安装

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

如果不是vue3以上的先卸载再安装,我当时卸载2.9.6版本的时候就是卸载了好多遍,然后还需要用管理员身份打开命令窗口安装vue4。

npm uninstall vue-cli -g  卸载

npm install -g @vue/cli  安装

我的版本:

二、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正确处理多行字符串


   
  1. output: {
  2. path: config.build.assetsRoot,
  3. filename: '[name].js',
  4. publicPath: process.env.NODE_ENV === 'production'
  5. ? config.build.assetsPublicPath
  6. : config.dev.assetsPublicPath,
  7. sourcePrefix: ' '
  8. },

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


   
  1. output: { ...}
  2. amd: {
  3. toUrlUndefined: true
  4. } ,

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


   
  1. resolve: {
  2. extensions: [ '.js', '.vue', '.json'],
  3. alias: {
  4. 'vue$': 'vue/dist/vue.esm.js',
  5. '@': resolve( 'src'),
  6. 'cesium': path. resolve(__dirname, cesiumSource)
  7. }
  8. },

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


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

3.配置webpack.dev.conf.js

①配置路径


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

②在plugins下面添加如下插件


   
  1. plugins: [
  2. ...,
  3. // Copy Cesium Assets, Widgets, and Workers to a static directory
  4. new CopyWebpackPlugin([{ from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' }]),
  5. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Assets'), to: 'Assets' }]),
  6. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' }]),
  7. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers' }]),
  8. new webpack.DefinePlugin({
  9. // Define relative base path in cesium for loading assets
  10. CESIUM_BASE_URL: JSON.stringify( '')
  11. })
  12. ]

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

①配置路径


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

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


   
  1. plugins: [
  2. ...,
  3. // Copy Cesium Assets, Widgets, and Workers to a static directory
  4. new CopyWebpackPlugin([{ from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' }]),
  5. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Assets'), to: 'Assets' }]),
  6. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' }]),
  7. new CopyWebpackPlugin([{ from: path.join(cesiumSource, 'ThirdParty/Workers'), to: 'ThirdParty/Workers' }]),
  8. new webpack.DefinePlugin({
  9. // 定义Cesium从哪里加载资源,如果使用默认的'',却变成了绝对路径了,所以这里使用'./',使用相对路径
  10. CESIUM_BASE_URL: JSON.stringify( './')
  11. })
  12. ]

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


   
  1. assetsSubDirectory: 'static',
  2. assetsPublicPath: '',

三、引用和启动

在main.js引用cesium

新建一个vue文件测试是否引用cesium成功


   
  1. <template>
  2. <div class="map">
  3. <div id="cesiumContainer"> </div>
  4. </div>
  5. </template>
  6. <script>
  7. let Cesium = require( 'cesium/Cesium') //cesium1.6版本以后导入的方
  8. //import CesiumViewer from './CesiumExample/No01-init.vue'
  9. import 'cesium/Widgets/widgets.css'
  10. export default{
  11. name: "home",
  12. data () {
  13. return {
  14. }
  15. },
  16. mounted(){
  17. //Cesium.Ion.defaultAccessToken = config.serverWeb.AccessToken;//秘钥
  18. const viewer = new Cesium.Viewer( 'cesiumContainer')
  19. },
  20. }
  21. </script>
  22. <style scoped>
  23. .map{
  24. width: 100%;
  25. height: 100%;
  26. }
  27. </style>

 

效果:

参考:https://www.jianshu.com/p/ff26886f7255


转载:https://blog.csdn.net/weixin_44544845/article/details/116701714
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场