You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 14中使用@angular/google-maps报Cannot find namespace 'google'错误

解决Angular 14中Cannot find namespace 'google'的问题

方案一:安装谷歌地图类型定义文件

  • 执行以下命令安装依赖的TypeScript类型定义包:
    npm install @types/google.maps --save-dev
    
    该包会提供google命名空间的类型声明,让TypeScript能够识别相关类型。

方案二:配置TypeScript编译选项

全局配置(推荐)

在项目根目录的tsconfig.json文件中,找到compilerOptions字段,添加types数组并包含google.maps:

{
  "compilerOptions": {
    // 其他已有的编译配置...
    "types": ["google.maps"]
  }
}

局部配置

如果只想在特定组件中引入类型,可以在组件的.ts文件顶部添加以下引用:

/// <reference types="@types/google.maps" />

方案三:确认谷歌地图API脚本加载

确保在index.html的<head>或<body>中正确加载谷歌地图API脚本,替换YOUR_API_KEY为你的有效API密钥:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>

内容的提问来源于stack exchange,提问作者Nikhil Makwana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 22:48:52