Google One-tap声明全局变量L.

时间:2017-11-14 17:37:23

标签: javascript leaflet leaflet.markercluster google-identity

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

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

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

这与其他像LeafletJS这样定义全局L的库有冲突 https://github.com/Leaflet/Leaflet

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

我是Google的产品经理。现在应该修复。对此感到抱歉,感谢您的耐心等待。