【问题标题】:Cannot find namespace 'google'找不到命名空间“谷歌”
【发布时间】:2017-05-12 14:03:57
【问题描述】:

从 Ionic2 beta 升级到 RC3 后,

我跑了:

npm install --save @types/google-maps

在我的package.json 中添加了以下内容:

"@types/google-maps": "^3.2.0",

我的index.html 有:

  <script src="http://maps.google.com/maps/api/js"></script>
  <script src="cordova.js"></script>

我的代码包含:

declare var google: any;
...
public map: google.maps.Map = null;

但是当我运行ionic serve 时出现以下错误:

Cannot find namespace 'google'.

在我的 IDE (VSC) 中,如果我单击 google,它会将我带到 node_modules\@types\googlemaps\index.d.ts,这似乎具有正确的定义。

任何想法表示赞赏。

【问题讨论】:

  • 也许你忘了npm install google-maps --save
  • @SurajRao。我刚刚尝试过:npm install google-maps --save。它似乎已将"google-maps": "^3.2.1" 添加到我的package.json 中,我想我还是需要它,谢谢。但是当我尝试运行 ionic serve 时,我仍然遇到同样的错误。

标签: google-maps ionic2


【解决方案1】:

实际上离子原生已经有谷歌地图。你可以去看看here

实际的插件是here

ionic cordova plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"

【讨论】:

  • 谢谢,这看起来像我所缺少的。如果可行,我会试一试并将其标记为答案。
  • 谢谢。这样可行。这就是 Ionic 2 的较新版本应该如何完成的。ionicframework.com/docs/v2/native/google-maps
  • @Richard,您是否尝试过使用此代码在设备(离子运行 android)上测试您的应用程序?在我的情况下,设备上没有显示地图。谢谢
【解决方案2】:

未来的读者可能不会像您解决问题那样幸运。从路径中的反斜杠可以清楚地看出您在 Windows 上。在这种情况下,my answer to a similar question 的这段摘录可能会很有趣:

您使用的是 ionic,并且在某些情况下存在平台差异 情况(不确定谁负责,离子或打字稿) 相同的 tsconfig.json 文件可以在 macOS(也许还有 linux)上运行,但是 在没有明确添加 typeRoots 条目的情况下,不在 Windows 中 你的 tsconfig.json 的 compilerOptions,像这样:

// tsconfig.json
{
  "compilerOptions": {
    // ... other stuff
    "typeRoots": [ 'node_modules/@types' ]
  }
  // ... other stuff
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2015-06-20
    • 2012-07-14
    • 2011-03-10
    • 2017-08-13
    • 2019-08-31
    相关资源
    最近更新 更多