1. 命令行在项目目录下,安装cesium组件库 npm install cesium --save
2. 在build/webpack.base.conf.js下,
定义cesium源码路径
const cesiumSource = '../node_modules/cesium/Source'
var webpack = require('webpack')
在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: ' '
},
在resolve中设置cesium别名
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
'cesium': path.resolve(__dirname, cesiumSource)
}
},
module里添加
module: {
rules: [...],
unknownContextCritical: false,
},
3. 配置 webpack.dev.conf.js 文件
配置路径
const cesiumSource = 'node_modules/cesium/Source'
const cesiumWorkers = '../Build/Cesium/Workers'
在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({
// 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配置略有不同
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 从哪里加载资源,如果使用默认的'',却变成了绝对路径了,所以这里使用'./',使用相对路径
CESIUM_BASE_URL: JSON.stringify('./')
})
5. 修改config里index.js中build中的assetsPublicPath为"./"
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: "./",
6. 配置main.js
import Cesium from 'cesium/Cesium'
import widgets from 'cesium/Widgets/widgets.css'
Vue.prototype.Cesium = Cesium
Vue.prototype.widgets = widgets
7. div引入,使用cesium。修改components下的HelloWorld.vue组件
8. 执行npm run dev , 打开浏览器运行项目(localhost:8010)(默认端口号为8080,到config/index.js里可修改)