我在 package.json 中有以下內容
"@types/google.maps": "~3.48.3",
// 在我的 tsx 檔案中,頂部的第一行是
/// <reference types='google.maps' />
但是在訪問 google.maps.MapMouseEvent - 我得到錯誤“谷歌”未定義。第 980:43 行:“google”未定義 no-undef
980 號線是
onMarkerDragEnd = async (mapMouseEvent: google.maps.MapMouseEvent) => {
// 我也試過
/// <reference path='../../../../node_modules/@types/google.maps' />
// 我也在 tsconfig.json 中嘗試過
"compilerOptions": {
"types": [
"google.maps"
]
}
但錯誤仍然存??在。我正在使用編譯打字稿
react-app-rewired build
尋找有關如何在打字稿中使用 @types/google.maps 的任何提示。
uj5u.com熱心網友回復:
no-undef實際上是一個 eslint 錯誤,可以使用https://eslint.org/docs/user-guide/configuring/language-options#specifying-globals修復
{
"globals": {
"google": "readonly"
}
}
使用最新版本的 typescript,除了 package.json 中的以下內容之外,您不需要任何其他內容。
devDependencies: {"@types/google.maps": ...}
沒有三斜杠參考,沒有匯入,沒有 compilerOptions.types。
有關示例,請參見https://codesandbox.io/embed/github/googlemaps/js-samples/tree/sample-marker-simple 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/454112.html
