TypeScript React Google Maps:'google'不存在

问题描述 投票:1回答:1

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

enter image description here

但是TypeScript不知道它在那里,并且出现此错误Property 'google' does not exist on type 'Window & typeof globalThis'.ts(2339)我怎么知道它确实存在?

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 };
reactjs typescript google-maps
1个回答
0
投票

我考虑了更多。如果您尚未从DefinitelyTyped安装Google Map类型,则建议这样做:

nom install --save @types/googlemaps。 (如果您只希望它们用于自己的开发,则为--save-dev

© www.soinside.com 2019 - 2024. All rights reserved.