reactjs - TypeScript React 谷歌地图 : 'google' does not exist

标签 reactjs typescript google-maps

我已经在 public/index.html 中加载了 google maps Javascript API,在开发工具控制台中我可以正常登录 window.google.maps

enter image description here

但是 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/

相关文章:

javascript - 更漂亮的 javascript 代码缩进配置

javascript - setInterval 的时钟不更新时间

javascript - 使用 Typescript Angular 中的接口(interface)检查 API 响应数据

typescript - 在 typescript 中,类型 `object` 和 `Record<any, any>` 之间有区别吗?

ios - 如何正确地将值传递给谷歌地图 url 方案回调?

google-maps - 如何在谷歌地图中绘制海上航线?

asp.net - 谷歌地图使用多边形突出显示带边界的国家/地区

javascript - 可恢复上传在 Android 后台失败 - React Native

reactjs - 在 Office-ui-Fabric 中的 DetailsLists 组件的同一行添加 EDIT 按钮 - ReactJS

css - 如何访问样式化组件的 props 函数内的 CSS 变量?