打字稿错误:环境模块声明仅在文件中的顶级允许
目前正在尝试从Google服务获取数据。 我不确定在声明global
时如何处理此错误。
链接到原始代码段: https:// https://stackblitz.com/edit/githit/githiit/githubblit/githubub -mge1vg?file = index.ts
如何解决此问题,为什么要遇到错误。
代码的这一部分引发了一个错误:
仅在文件中的最高级别允许环境模块声明。
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
这是整个代码:
import {useEffect, useState} from 'react';
type Geocode = {
data?: google.maps.GeocoderResult | null;
loading: boolean;
error: string | null;
};
export const useReverseGeoCoding = ({latitude, longitude}: {latitude: number; longitude: number}): Geocode => {
const [geocode, setGeocode] = useState<Geocode>({loading: false, error: null});
useEffect(() => {
const geocodeLatLng = async (geocoder: google.maps.Geocoder) => {
/// Hard Coded for now
const latlng = {
lat: 52.509865,
lng: -0.118092,
};
geocoder
.geocode({location: latlng})
.then(response => {
if (response.results[0]) {
console.log('response:', response);
setGeocode({data: response.results[0], loading: false, error: null});
} else {
console.log('No results found');
}
})
.catch(error => console.log('Geocoder failed due to: ' + error));
};
const initMap = (): void => {
const geocoder = new google.maps.Geocoder();
geocodeLatLng(geocoder);
};
initMap();
}, [latitude, longitude]);
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
return geocode;
};
Currently trying to fetch data from a google service.
I'm not sure how I should handle this error when declaring global
.
Link to the original code snippet:https://stackblitz.com/edit/github-mge1vg?file=index.ts
How would solve this and why am I getting the error.
This part of the code throws an error:
An ambient module declaration is only allowed at the top level in a file.
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
Here is the whole code:
import {useEffect, useState} from 'react';
type Geocode = {
data?: google.maps.GeocoderResult | null;
loading: boolean;
error: string | null;
};
export const useReverseGeoCoding = ({latitude, longitude}: {latitude: number; longitude: number}): Geocode => {
const [geocode, setGeocode] = useState<Geocode>({loading: false, error: null});
useEffect(() => {
const geocodeLatLng = async (geocoder: google.maps.Geocoder) => {
/// Hard Coded for now
const latlng = {
lat: 52.509865,
lng: -0.118092,
};
geocoder
.geocode({location: latlng})
.then(response => {
if (response.results[0]) {
console.log('response:', response);
setGeocode({data: response.results[0], loading: false, error: null});
} else {
console.log('No results found');
}
})
.catch(error => console.log('Geocoder failed due to: ' + error));
};
const initMap = (): void => {
const geocoder = new google.maps.Geocoder();
geocodeLatLng(geocoder);
};
initMap();
}, [latitude, longitude]);
declare global {
interface Window {
initMap: () => void;
}
}
window.initMap = initMap;
return geocode;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
移动
主函数有效,然后
在文件中调用您将使用钩子,然后添加脚本标签以初始化Google API
Moving
Outside of the main function works, then calling
Within the file you will use the hook, and add a script tag to initialize the Google API