Vue?運行高德地圖官方樣例,設置class無效的解決
vue 使用高德地圖,網(wǎng)上有很多樣例,還是選擇官方樣例吧,做的挺不錯的。
可以下載官方樣例,在里邊添加各種api來測試

添加各種api的方法,要在.then((AMap)里增加,我剛開始放在外層,是錯誤的
把對應的代碼貼一下
<template>
<div class="home_div">
<div class="map_title">
<h3>JSAPI Vue2地圖組件示例</h3>
</div>
<div id="container"></div>
</div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
export default {
name: "Mapview",
data() {
return {
//map:null,
};
},
created() {},
mounted() {
this.initAMap();
},
methods: {
initAMap() {
AMapLoader.load({
key: "設置自己的key", //設置您的key
version: "2.0",
plugins: ["AMap.ToolBar", "AMap.Driving"],
AMapUI: {
version: "1.1",
plugins: [],
},
Loca: {
version: "2.0",
},
})
.then((AMap) => {
var map = new AMap.Map("container", {
viewMode: "3D",
zoom: 5,
zooms: [2, 22],
center: [105.602725, 37.076636],
});
var marker = new AMap.Marker({
position: map.getCenter(),
icon: "http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
anchor: "bottom-center",
offset: new AMap.Pixel(0, 0),
});
marker.setMap(map);
// 設置鼠標劃過點標記顯示的文字提示
marker.setTitle("我是marker的title");
// 設置label標簽
// label默認藍框白底左上角顯示,樣式className為:amap-marker-label
marker.setLabel({
direction: "top",
offset: new AMap.Pixel(10, 0), //設置文本標注偏移量
content: "<div class='info'>我是 marker 的 label 標簽</div>", //設置文本標注內容
});
})
.catch((e) => {
console.log(e);
});
},
},
};
</script><style >
.home_div {
padding: 0px;
margin: 0px;
width: 100%;
height: 100%;
position: relative;
}
#container {
padding: 0px;
margin: 0px;
width: 100%;
height: 100%;
position: absolute;
}
.map_title {
position: absolute;
z-index: 1;
width: 100%;
height: 50px;
background-color: rgba(27, 25, 27, 0.884);
}
h3 {
position: absolute;
left: 10px;
z-index: 2;
color: white;
}
.amap-icon img {
width: 25px;
height: 34px;
}
.amap-marker-label {
border: 0;
background-color: transparent;
}
.info {
position: relative;
margin: 0;
top: 0;
right: 0;
min-width: 0;
}
</style>如果用官方樣例,設置內置樣式,是無效的,比如設置 marker的Label樣式,這個內置的樣式是amap-marker-label,但是設置了是無效的,原因是<style scoped>,官方樣例加了scoped,會把此style限定在當前的頁面中。
在編譯時,有scoped的頁面樣式,都會自動生成有一個唯一標識(attribute),這樣,用字符串方式添加的標簽只會是單獨的標簽而缺少這些標識導致css設置無效。
解決辦法
1.樣式直接添加到index.html中
index.html中的標簽會是一個全局標簽,添加到這里會直接有效。
2.樣式不使用 scoped
不添加scoped在編譯時就不會有唯一標識,這些css也是全局有效,但是全局標簽存在一些風險,比如兩個頁面寫了同名的之類。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue代理請求之Request?failed?with?status?code?404問題及解決
這篇文章主要介紹了vue代理請求之Request?failed?with?status?code?404問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue3使用sessionStorage保存會話數(shù)據(jù)的實現(xiàn)方式
在前端開發(fā)中,我們常常需要在用戶會話期間保存一些數(shù)據(jù),這些數(shù)據(jù)在頁面刷新或導航時依然需要存在,sessionStorage 是一種非常方便的方式來實現(xiàn)這一點,在這篇文章中,我們將探討如何在Vue3應用中使用sessionStorage來保存會話數(shù)據(jù),需要的朋友可以參考下2025-01-01
vue 數(shù)據(jù)雙向綁定的實現(xiàn)方法
這篇文章主要介紹了vue 數(shù)據(jù)雙向綁定的實現(xiàn)方法,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下2021-03-03
Vue.js條件渲染和列表渲染以及Vue中key值的內部原理
這篇文章主要介紹了Vue.js條件渲染和列表渲染,以及Vue中key值的內部原理,文中有詳細的代碼示例,感興趣的同學可以參考閱讀2023-04-04
Vue純前端實現(xiàn)導出excel中的圖片與文件超鏈接
這篇文章主要為大家詳細介紹了Vue如何純前端實現(xiàn)導出excel中的圖片與文件超鏈接,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2025-03-03

