Vue+Cesium环境搭建

基础环境安装

node.js(检测npm包管理工具  npm -v)
Webpack安装(全局安装  npm install webpack -g)(检测安装成功: webpack -v)
vue-cli脚手架构建工具(npm install vue-cli -g)(检测:vue -V)

vue项目搭建

命令行cd进入项目构建文件目录级,执行vue init webpack project_name(项目名不可用中文),按提示输入相应内容,项目文件构建完成。
cd到项目目录内,安装项目依赖npm install,项目启动npm run dev
浏览器进入地址http://localhost:8080,打开项目页面,搭建完成

Cesium环境搭建
命令行在项目目录下,安装cesium组件库 npm install cesium --save

修改 在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,
},

修改 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('')
})

修改 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('./')
})

修改config里index.js中build中的assetsPublicPath为"./"

// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: "./",

配置main.js

import Cesium from 'cesium/Cesium'
import widgets from 'cesium/Widgets/widgets.css'
 
Vue.prototype.Cesium = Cesium
Vue.prototype.widgets = widgets

div引入,使用cesium。修改components下的HelloWorld.vue组件


<template>
  <div id="container" class="box">
    <div id="cesiumContainer"></div>
  </div>
</template>
<script>

import Cesium from 'cesium/Cesium'

export default {
  name: 'cesiumPage',
  data () {
    return {
    }
  },
  mounted(){
    //var Cesium = this.Cesium;
    var cesiumContainer = document.getElementById("cesiumContainer");
    var viewer = new Cesium.Viewer("cesiumContainer",{
      //加载在线谷歌地图
      imageryProvider: new Cesium.UrlTemplateImageryProvider({
         url:"http://www.google.cn/maps/vt?lyrs=s&x={x}&y={y}&z={z}"
      })
    })
  }
}
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
html,
body,
#cesiumContainer {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
 #app {
  font-family: "Avenir" , Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  width: 100%;
  height: 100%;
}
.box {
  height: 100%;
} 
</style>

执行npm run dev , 打开浏览器运行项目(localhost:8081)(默认端口号为8080,到config/index.js里可修改)

这里提示构造CesiumWidget时出错
在这里有两种解决办法
第一种是修改 build/webpack.base.conf.js,
module里添加

module: {
   rules: [...],
   unknownContextCritical: false,
   unknownContextRegExp: /^.\/.*$/,
},

第二种还是修改build/webpack.base.conf.js,

output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath,
      sourcePrefix: ' '
  },
  //在Cesium中启用Webpack时使用Require
   amd: {                                    
     toUrlUndefined: true
   },

我个人倾向于第二种0.0;
修改完之后运行 如下

你可能感兴趣的:(Cesium)