Vue+Cesium快速搭建的方法步驟(無需配置)
方式一:直接引入(最簡單)
1.安裝Cesium(Vue搭建可以看我上一期的文章)
npm i cesium -save
2.將node_modules\cesium\Build\Cesium文件夾拷貝到項(xiàng)目的public文件中
3.在public\index.html引入Cesium
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico" rel="external nofollow" >
<title><%= htmlWebpackPlugin.options.title %></title>
<!--在這里引入Cesium和css文件-->
<script src="Cesium/Cesium.js"></script>
<link href="Cesium/Widgets/widgets.css" rel="external nofollow" rel="stylesheet">
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
4.加載Cesium
當(dāng)我們在index.html中引入了Cesium.js后,默認(rèn)Cesium對象就掛載到了Window對象上面,在Vue文件中加載:
<template>
<div class="home">
<div id="cesiumContainer"></div>
</div>
</template>
<script>
export default {
data() {
return {
map: {}
}
},
components: {},
created() {},
mounted() {
const viewer = new window.Cesium.Viewer('cesiumContainer')
console.log(viewer)
},
computed: {},
methods: {
initMap() {}
}
}
</script>
<style scoped lang="scss">
.home {
width: 100%;
height: 100%;
}
#cesiumContainer {
height: 100%;
width: 100%;
}
</style>
5.運(yùn)行后我們可能會(huì)遇到報(bào)錯(cuò)Failed to resolve loader: sass-loader
需要引入 sass-loader 和 node-sass 包:
cnpm install sass-loader -D cnpm install node-sass -D
6.接下來就可以看到地球啦!

方式二:使用vue-cli-plugin-cesium 插件安裝cesium(需要申請key)
(因?yàn)椴寮趙ebpack的,當(dāng)前插件只支持 VueCLI3.0+ 版本)
1.安裝vue-cli-plugin-cesium
vue add vue-cli-plugin-cesium
2.但最后結(jié)果都會(huì)報(bào)錯(cuò):ValidationError: Invalid options object. Copy Plugin has been initialized using an options object that does not match the API schema.
3.經(jīng)多次踩坑之后發(fā)現(xiàn),一切問題都是安裝的vue-cli腳手架版本太高所致。
卸載vue-cli2:npm uninstall vue-cli -g卸載vue-cli3:npm uninstall @vue/cli -g
vue安裝:
npm install -g @vue/cli (安裝的是最新版) npm install vue-cli@2.9.6 (指定版本為3.0以下版本) npm install -g @vue/cli@4.5.17(指定版本為3.0以上版本)
其他博主4.5.17版本的vue-cli安裝cesium可以成功,所以我就也安裝了這個(gè)版本。
4.新建項(xiàng)目,重新安裝vue-cli-plugin-cesium
安裝完成后運(yùn)行可能會(huì)遇到:Error: error:0308010c:digital envelope routines::unsupported [Node Error Solved]修改你的node版本為16.16.0
5.在vue項(xiàng)目中打開main.js,添加在cesium官網(wǎng)申請到的key值:
import { createApp } from 'vue'
import App from './App.vue'
import 'cesium/Widgets/widgets.css'
Cesium.Ion.defaultAccessToken = '申請的key值';
createApp(App).mount('#app')
6.運(yùn)行項(xiàng)目即可:
npm run serve
到此這篇關(guān)于Vue+Cesium快速搭建的方法步驟(無需配置)的文章就介紹到這了,更多相關(guān)Vue Cesium搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Element UI 自定義正則表達(dá)式驗(yàn)證方法
今天小編就為大家分享一篇Element UI 自定義正則表達(dá)式驗(yàn)證方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue中watch監(jiān)聽對象中某個(gè)屬性的方法
watch 的用法有個(gè)特點(diǎn),就是當(dāng)值第一次綁定的時(shí)候,不會(huì)執(zhí)行監(jiān)聽函數(shù),只有值發(fā)生改變才會(huì)執(zhí)行,如果我們需要在最初綁定值得時(shí)候也執(zhí)行函數(shù),就需要用到 immediate 屬性,這篇文章主要介紹了vue中watch監(jiān)聽對象中某個(gè)屬性的方法,需要的朋友可以參考下2023-04-04
vue用戶長時(shí)間不操作退出到登錄頁的兩種實(shí)現(xiàn)方式
出于安全考慮,用戶長時(shí)間不操作,就回到登錄頁面,讓用戶重新登錄,本文就記錄一下實(shí)現(xiàn)這種效果的兩種方式,具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09
Vue使用Echarts圖表多次初始化報(bào)錯(cuò)問題的解決方法
最近在學(xué)習(xí)Vue的時(shí)候,正好學(xué)到了引入echarts圖表做數(shù)據(jù)統(tǒng)計(jì)的內(nèi)容,所以下面這篇文章主要給大家介紹了關(guān)于Vue使用Echarts圖表多次初始化報(bào)錯(cuò)問題的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
如何使用 vue-cli 創(chuàng)建模板項(xiàng)目
這篇文章主要介紹了如何使用 vue-cli 創(chuàng)建模板項(xiàng)目,幫助大家更好的理解和學(xué)習(xí)vue框架的知識,感興趣的朋友可以了解下2020-11-11

