vue create test
cnpm install cesium --save
需要修改webpack.base.conf.js, webpack.dev.conf.js, webpack.prod.conf.js 三个文件
4.1修改webpack.base.conf.js
4.1.1定义 Cesium 源码路径:
const cesiumSource = '../node_modules/cesium/Source' //定义 Cesium 源码路径
const cesiumWorkers = path.join(cesiumSource,'Workers');
4.1.2让webpack正确处理多行字符串,在output中添加sourcePrefix:' '
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath,
sourcePrefix: ' ' //让webpack正确处理多行字符串
},
4.1.3 设置cesium别名 ,cesium: path.resolve(__dirname, cesiumSource)//设置cesium别名
需要在resolve中设置cesium别名,这样在引入的时候就可以根据别名找到Cesium的包。
(注:也可以不设置别名,导包是直接导入'cesium/Source/Cesium.js'就行。其实设置别名的目的就是让“别名”指向/node_modules/cesium/Source目录)
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
cesium: path.resolve(__dirname, cesiumSource) //设置cesium别名
}
},
4.1.4阻止依赖警告
module里添加unknownContextCritical:false
module: {
rules:[
...
],
unknownContextCritical: false,//阻止依赖警告
}
4.2 配置 webpack.dev.conf.js
4.2.1定义路径:
const cesiumSource = 'node_modules/cesium/Source'
const cesiumWorkers = '../Build/Cesium/Workers'
4.2.2在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.3 配置webpack.prod.conf.js文件
4.3.1定义路径:
const cesiumSource = 'node_modules/cesium/Source'
const cesiumWorkers = '../Build/Cesium/Workers'
4.3.2在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({
// 注意这里和dev的配置不同
// 定义Cesium从哪里加载资源,如果使用默认的'',却变成了绝对路径了,所以这里使用'./',使用相对路径
CESIUM_BASE_URL: JSON.stringify('./')
})
var Cesium = require('cesium/Cesium');
var widgets= require('cesium/Widgets/widgets.css');
Vue.prototype.Cesium = Cesium
Vue.prototype.widgets = widgets
或者在组件中引入
import * as Cesium from "@/../node_modules/cesium/Source/Cesium.js"
//也可以用按需引入的方式,具体路径需要自己调整
import Viewer from "cesium/Source/Widgets/Viewer/Viewer";
新版本不支持整体import,可以按需引入。
改用如下方式引入
var Cesium = require('cesium/Cesium');
var widgets= require('cesium/Widgets/widgets.css');
Vue.prototype.Cesium = Cesium
Vue.prototype.widgets = widgets
npm i cesium-navigation-es6 -S
* cesium in ./node_modules/cesium-navigation-es6/CesiumNavigation.js, ./node_modules/cesium-navigation-es6/core/registerKnockoutBindings.js and 8 others
npm i [email protected] -S
"export 'default' (imported as 'Cesium') was not found in 'cesium/Cesium'
替换import cesium from 'cesium/cesium'为 let Cesium = require('cesium/Cesium')
import CesiumNavigation from 'cesium-navigation-es6/viewerCesiumNavigationMixin'
只需要将defineProperties改为Object.defineProperties就可以了