Kyl*_* VG 4 google-maps typescript eslint
const google = window.google;
let markers: google.maps.Marker[] = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener("places_changed", () => {
const places = searchBox.getPlaces();
if (places.length === 0) {
return;
}
// Clear out the old markers.
markers.forEach((marker) => {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
const bounds = new google.maps.LatLngBounds();
places.forEach((place) => {
if (!place.geometry || !place.geometry.location) {
return;
}
const icon = {
url: place.icon as string,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25),
};
// Create a marker for each place.
markers.push(
new google.maps.Marker({
map,
icon,
title: place.name,
position: place.geometry.location,
})
);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
Run Code Online (Sandbox Code Playgroud)
这是来自谷歌地图 API 的一些代码。它编译并运行得很好。
问题是,当 linter 检查它时,它会给出以下错误:
222:22 error 'google' is not defined no-undef
我const google = window.google;按照这个答案的解释添加到代码的顶部。使用 create-react-app 在 React 应用程序中未定义 google
但这仅对使用“new”关键字的情况有帮助。
let markers: google.maps.Marker[] = [];对于将类型分配给变量的这种情况没有帮助。
使用 eslint,您可以配置全局。https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals
{
"globals": {
"google": "readonly"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1528 次 |
| 最近记录: |