首页 > 解决方案 > 传单反应中的 openPopup 出错

问题描述

我在反应时面临传单的问题。我希望当我点击卡片时,我会在我点击的地方弹出,我写了这段代码。它通常在 javascript 中工作,但在 React 中引发错误

this.map.on('click', async function (e) {
            var coord = e.latlng;
            var lat = coord.lat;
            var lng = coord.lng;
            var popup = L.popup({minWidth: 30, maxHeight: 300})
                .setLatLng([50.51754, 136.94726])
                .setContent(`test`)
                .openOn(this.map);

        });

单击卡片时出现此错误

   Unhandled Rejection (TypeError): Cannot read property 'openPopup' of undefined
  104 | // @method openOn(map: Map): this
  105 | // Adds the popup to the map and closes the previous one. The same as `map.openPopup(popup)`.
  106 | openOn: function (map) {
> 107 |     map.openPopup(this);
      | ^  108 |    return this;
  109 | },
  110 | 

谢谢!

标签: javascript

解决方案


推荐阅读