Skip to main content

vue3+cesium 环境搭建

  1. 安装 vue3

    npm i -g @vue/cli 
    vue -V  / vue --version
    //如果已经安装vue2 需要先卸载,再进行vue3的安装 
  2. 创建项目

    vue create proName 
    cd proName
    npm run serve //可以在 package.json文件中,scripts 中新增 start
    // 如果需要创建vue2版本的项目需要 安装  
    // npm install -g @vue/cli-init
    // vue init webpack proName
  3. 安装cesium 依赖

    npm i cesium --save
  4. 在项目根目录新增配置文件 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: 8090 //修改服务端口号
    },
    outputDir: 'docs', //设置 build 输出目录
    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
    
        }
    }
    }
  5. main.js 中新增引用

    import Cesium from 'cesium/Cesium' //from 中 cesium 指向的是vue.config.js中 alias对象中的别名
    // noinspection ES6UnusedImports
    import widget from 'cesium/Widgets/widgets.css'

发表回复