我已经在 public/index.html
中加载了 google maps Javascript API,在开发工具控制台中我可以正常登录 window.google.maps
。
但是 TypeScript 不知道它在那里,我得到了这个错误存在吗?
import React, { useRef } from "react";
function Map() {
const mapRef = useRef<HTMLDivElement>(null);
const google = window.google;
// ^ Error: Property 'google' does not exist on type 'Window & typeof globalThis'
const myLatlng = new google.maps.LatLng(-34.397, 150.644);
const mapOptions = {
zoom: 8,
center: myLatlng,
};
const map = new google.maps.Map(mapRef.current, mapOptions);
return (<div ref={mapRef}></div>);
}
export { Map };
最佳答案
我又想了想。如果您还没有安装来自 DefinitelyTyped 的 Google map 类型,我建议您这样做:
npm install --save @types/googlemaps
。 (或者 --save-dev
如果您只希望它们用于您自己的开发。)
关于reactjs - TypeScript React 谷歌地图 : 'google' does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60986424/