Vue?運行高德地圖官方樣例,設(shè)置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: "設(shè)置自己的key", //設(shè)置您的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); // 設(shè)置鼠標劃過點標記顯示的文字提示 marker.setTitle("我是marker的title"); // 設(shè)置label標簽 // label默認藍框白底左上角顯示,樣式className為:amap-marker-label marker.setLabel({ direction: "top", offset: new AMap.Pixel(10, 0), //設(shè)置文本標注偏移量 content: "<div class='info'>我是 marker 的 label 標簽</div>", //設(shè)置文本標注內(nèi)容 }); }) .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>
如果用官方樣例,設(shè)置內(nèi)置樣式,是無效的,比如設(shè)置 marker的Label樣式,這個內(nèi)置的樣式是amap-marker-label,但是設(shè)置了是無效的,原因是<style scoped>,官方樣例加了scoped,會把此style限定在當前的頁面中。
在編譯時,有scoped的頁面樣式,都會自動生成有一個唯一標識(attribute),這樣,用字符串方式添加的標簽只會是單獨的標簽而缺少這些標識導致css設(shè)置無效。
解決辦法
1.樣式直接添加到index.html中
index.html中的標簽會是一個全局標簽,添加到這里會直接有效。
2.樣式不使用 scoped
不添加scoped在編譯時就不會有唯一標識,這些css也是全局有效,但是全局標簽存在一些風險,比如兩個頁面寫了同名的之類。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用WebSocket+SpringBoot+Vue搭建簡易網(wǎng)頁聊天室的實現(xiàn)代碼
這篇文章主要介紹了使用WebSocket+SpringBoot+Vue搭建簡易網(wǎng)頁聊天室的實現(xiàn),具體的步驟如下,需要的朋友可以參考下2023-03-03setup+ref+reactive實現(xiàn)vue3響應式功能
這篇文章介紹了通過setup+ref+reactive實現(xiàn)vue3響應式功能,文中通過示例代碼介紹的非常詳細。對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-11-11