最简单的方法是使用vue-cli-plugin-cesium插件
安装方式:
vue add vue-cli-plugin-cesium
此时运行会出现如下的错误:
ERROR ValidationError: Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
- options[0] misses the property 'patterns'. Should be:
......
降低copy-webpack-plugin的版本到4.0.1,
......
"dependencies": {
"cesium": "1.70.0",
"copy-webpack-plugin": "^4.0.1",
"core-js": "^3.8.3",
"vue": "^3.2.13"
}
......
运行运行npm install,然后再npm run serve即可,此时cesium示例就可以跑起来了。
此时项目虽然是可以运行的,但是如果你在JavaScript代码里面使用Cesium变量,还是会出现如下的错误。
error ‘Cesium‘ is not defined no-undef
这是因为vue找不到Cesium这个全局变量导致的,解决办法是打开vue.config.js文件,添加如下配置即可:
module.exports = defineConfig({
......
configureWebpack: {
devtool: 'source-map',
externals: {
"Cesium": "Cesium"
},
},
......
})
这时候运行yarn serve命令,仍然会出现如下的错误
ERROR in ./node_modules/@cesium/engine/Source/Core/Resource.js 1877:57-71
Module not found: Error: Can't resolve 'http' in '/Users/menglikun/git/village-web/node_modules/@cesium/engine/Source/Core'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "http": require.resolve("stream-http") }'
- install 'stream-http'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "http": false }
解决办法是先安装node-polyfill-webpack-plugin
npm install node-polyfill-webpack-plugin
然后修改vue.config.js文件,添加node-polyfill-webpack-plugin插件即可
// 头部引入
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
module.exports = defineConfig({
transpileDependencies: true,
devServer: {
port: 8380, // 端口号
},
configureWebpack: {
devtool: 'source-map',
externals: {
"Cesium": "Cesium"
},
plugins: [new NodePolyfillPlugin()] //添加插件
}
})
评论区