【问题标题】:Google One-tap declares global variable LGoogle One-tap 声明全局变量 L
【发布时间】:2017-11-14 17:37:22
【问题描述】:

Google One-tap 库在与其他库一起使用时会导致错误,因为它声明了一个全局变量 L。

我在缩小的代码中看到了这一点,但我不确定它在做什么。

var L = 0;
function K(g) {
  return "jscomp_symbol_" + (g || "") + L++
}

这与也定义了全局 L 的 LeafletJS 等其他库冲突 https://github.com/Leaflet/Leaflet

我找不到 Google 的一键式代码提交补丁的开源项目。我该如何解决这个问题?

【问题讨论】:

  • 这是谷歌发布过程中的一个bug;它应该被包裹在一个 IIFE 中。
  • 我们目前正在修复该错误。
  • 我们正在努力解决这个问题,我会在它准备好时更新。对此感到抱歉,但感谢您的报告,如果您需要更多信息或有其他反馈,请随时联系 sso@google.com。
  • 这个问题已经修复,不再声明 L 变量。

标签: javascript leaflet leaflet.markercluster google-identity


【解决方案1】:

我是这个库的 Google 产品经理。现在应该解决这个问题。对此感到抱歉,感谢您的耐心等待。

【讨论】:

    【解决方案2】:

    在 Google One-tap 文件中的错误得到纠正之前,您可以简单地使用 Leaflet 的 noConflict 模式,以便您可以将 Leaflet 重新分配给您喜欢的任何其他全局变量名称,并避免使用 Google 库使用的那些。由于后者是一个缩小文件,因此大多数变量和函数的长度为 1 或 2 个字符。

    // Should actually be BELOW the HTML part,
    // but SO insists on displaying the JS part first.
    
    var map = Leaflet.map("map").setView([48.86, 2.35], 12);
    
    Leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css">
    <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet-src.js"></script>
    
    <script>
      // Re-assign L from Leaflet to any other global variable, and avoid conflict
      // with other libraries, like Google One-tap in this case.
      var Leaflet = L.noConflict();
    </script>
    
    <script src="https://smartlock.google.com/client"></script>
    
    <div id="map" style="height: 200px;"></div>

    【讨论】:

    • 谢谢@ghybs!不幸的是,我们正在使用多个 Leaflet 库,包括 Leaflet.markercluster。该库正在寻找 window.L 所以我不知道保存 Leaflet 库是否对我们有用。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2011-06-30
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多