首页 > 解决方案 > 在我的传单扩展中访问 VueJS “this”

问题描述

我正在我的 Vuejs 应用程序中使用 Leaflet,并创建了 L.Control 的扩展,如果我可以使用 Vuex 管理它的一部分,它可能会受益匪浅。我只是不完全确定如何在方法中正确获取this我的 Vue 应用程序L.Control.extend

现在,我正在定义let that = this;和使用that任何我想处理 Vuejs 数据的时间,但这是最好的方法吗?这有限制吗?

这是我当前如何实现此功能的精简示例。

mounted() {

    let that = this;

    L.Control.ZoomUserLocation = L.Control.extend({

    _zoomUserLocation: function() {
          navigator.geolocation.getCurrentPosition(
            function success(position) {
              let lat = position.coords.latitude;
              let lng = position.coords.longitude;

              if (that.userLocationMarker !== null) {
                that.userLocationMarker.remove();
              }
              that.userLocationMarker = L.marker([lat, lng]).addTo(map);
            },
            function error(error_message) {...}
          );
        } 
        else {
          map.setView([that.center.lat, that.center.lng], that.zoom);
        }
      },
    })
}

标签: javascriptvue.jsleaflet

解决方案


推荐阅读