添加css后没有出现反应叶

问题描述 投票:0回答:1

我在rc-tabs组件中有一个map组件

import React from "react";
import {Map as LeafMap, TileLayer, Marker, Popup} from "react-leaflet";

const Map = () => {

    return (
        <div className="leaflet-appearance">
            <LeafMap center={[59.95, 30.33]} zoom={11}>
                <TileLayer
                    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                />
                <Marker position={[59.95, 30.33]}>
                    <Popup>
                        A pretty CSS3 popup. <br/> Easily customizable.
                    </Popup>
                </Marker>
            </LeafMap>
        </div>
    )
};

export default Map

并且它被渲染。效果不好-未加载某些图块,其缩放比例有所不同,但可见。图片以某种方式工作react-leaflet:

我想修复它。我将一个CSS添加到我的index.html文件中

<!-- Leaflet Maps Styling  -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>

并在Map.css中设置宽度

.leaflet-appearance {
    height: 200px;
    width: 200px;
}

传单不起作用:

我该如何解决?

javascript reactjs leaflet react-leaflet rc-tabs
1个回答
0
投票

很显然,您没有正确设置地图的高度,并且代码中的leaflet.css导入也存在某些错误

这里应该是这样:

import React from "react";
import "rc-tabs/assets/index.css";
import "leaflet/dist/leaflet.css";
import "./styles.css";

import Tabs, { TabPane } from "rc-tabs";
import TabContent from "rc-tabs/lib/TabContent";
import ScrollableInkTabBar from "rc-tabs/lib/ScrollableInkTabBar";
import { Map as LeafMap, TileLayer, Marker, Popup } from "react-leaflet";

export default function App() {
  var callback = function(key) {};

  return (
    <Tabs
      defaultActiveKey="1"
      onChange={callback}
      renderTabBar={() => <ScrollableInkTabBar />}
      renderTabContent={() => <TabContent />}
    >
      <TabPane tab="tab 1" key="1">
        <LeafMap center={[59.95, 30.33]} zoom={11} style={{ height: "100vh" }}>
          <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
          <Marker position={[59.95, 30.33]}>
            <Popup>
              A pretty CSS3 popup. <br /> Easily customizable.
            </Popup>
          </Marker>
        </LeafMap>
      </TabPane>
      <TabPane tab="tab 2" key="2">
        second
      </TabPane>
      <TabPane tab="tab 3" key="3">
        third
      </TabPane>
    </Tabs>
  );
}

Demo

© www.soinside.com 2019 - 2024. All rights reserved.