iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue 运行高德地图官方样例,设置class无效的解决
  • 435
分享到

Vue 运行高德地图官方样例,设置class无效的解决

Vue运行高德地图Vue设置class无效Vue设置class 2022-11-13 18:11:19 435人浏览 薄情痞子
摘要

目录把对应的代码贴一下解决办法1.样式直接添加到index.html中2.样式不使用 scopedVue 使用高德地图,网上有很多样例,还是选择官方样例吧,做的挺不错的。 jsapi

Vue 使用高德地图,网上有很多样例,还是选择官方样例吧,做的挺不错的。

jsapi结合Vue使用

可以下载官方样例,在里边添加各种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: "//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也是全局有效,但是全局标签存在一些风险,比如两个页面写了同名的之类。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Vue 运行高德地图官方样例,设置class无效的解决

本文链接: https://www.lsjlt.com/news/169532.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作