【发布时间】:2021-11-19 22:28:18
【问题描述】:
我一直在尝试将 Leaflet.js 库与 react.js 一起使用并遵循以下代码:
import {L} from 'leaflet'
const myApp = () => {
const map = L.map("map").setView([45, 1], 4);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'}).addTo(map);
L.control.scale().addTo(map);
L.marker([45, 1], {icon: new L.Icon({iconUrl: 'https://unpkg.com/leaflet@1.6.0/dist/images/marker-icon.png'})}).addTo(map);
return (
<div id="map" style="height:100vh"></div>
)}
我收到以下类型错误: TypeError: Cannot read properties of undefined (reading 'map') in line:
const map = L.map("map").setView([45, 1], 4);
有什么办法解决吗?
【问题讨论】:
标签: javascript reactjs