TypeScript错误:找不到命名空间'google'

时间:2016-10-07 04:43:37

标签: angular typescript angular-cli typescript-typings

我有项目angular-cli

〜根〜/ SRC / typings.json

var theError: NSError?
do {
     try managedObjectContext!.save()
 } catch {

     theError = error 
     print("Unresolved error \(theError), \(theError.userInfo)")
}

〜根〜/分型/ index.d.ts

{
  "globalDevDependencies": {
    "angular-protractor": "registry:dt/angular-protractor#1.5.0+20160425143459",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "selenium-webdriver": "registry:dt/selenium-webdriver#2.44.0+20160317120654"
  },
  "globalDependencies": {
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504",
    "google.maps": "registry:dt/google.maps#3.20.0+20160914131659"
  }
}

〜根〜/ SRC / tsconfig.json

/// <reference path="globals/angular-protractor/index.d.ts" />
/// <reference path="globals/es6-shim/index.d.ts" />
/// <reference path="globals/google.maps/index.d.ts" />
/// <reference path="globals/hammerjs/index.d.ts" />
/// <reference path="globals/jasmine/index.d.ts" />
/// <reference path="globals/selenium-webdriver/index.d.ts" />

运行 ng serve 后 我在控制台中有错误消息

  

[默认]中的错误   F:〜根〜\ SRC \应用\ UI \谷歌地图\地图标记\地图marker.directive.ts:7:26

     

找不到命名空间'google'

  

[默认]中的错误   〜根〜\ SRC \应用\跳闸入门页面\跳闸入门page.component.ts:188:21

     

找不到名称'google'

〜根〜\ SRC \应用\ UI \谷歌地图\地图标记\地图marker.directive.ts:7:26

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types",
      "../typings"
    ],
    "files": [
      "../typings/index.d.ts"
    ]
  }
}

〜根〜\ SRC \应用\跳闸入门页面\跳闸入门page.component.ts:188:21

... 
@Input() veyoMapMarker: google.maps.MarkerOptions 
...

构建应用程序正确工作后

我如何解决此错误消息?

8 个答案:

答案 0 :(得分:22)

有点迟到的回复,但我使用 Angular CLI RC.0 时遇到了类似的问题。

原来我没有安装和导入打​​字,可以按照以下方式完成:

npm install --save-dev @types/googlemaps

import {} from '@types/googlemaps';

答案 1 :(得分:2)

尝试在节点提示符中运行以下命令...

typings install dt~google.maps --global --save

答案 2 :(得分:2)

对于 Angular 9.****

我尝试安装@types/googlemaps,但对我不起作用。

我将其降级为 "@types/googlemaps": "3.39.12" 然后工作正常。

这是我的代码

在 tsconfig.app.json 中(将 googlemaps 添加到 types 数组)

"types": [
      "googlemaps"
    ]

在应用模块.ts

import { AgmCoreModule } from '@agm/core';
.
.
.
.
 imports: [
    BrowserModule,
    AppRoutingModule,
    AgmCoreModule.forRoot({
      apiKey: '...KEY...',
      libraries: ['places']
    })
  ],

要降级@types/googlemaps,请转到项目根文件夹中的pakage.json 文件并将@types/googlemaps 版本更改为“3.39.12”。最好是删除文件形式

node_module -> @types -> googlemaps

然后在终端输入

npm install

答案 3 :(得分:2)

1- 您需要做的就是通过以下方式导入包:

npm install @types/google-maps --save

2 - 在我的 tsconfig.app.json 中添加 "types": [ "googlemaps" ]。 .现在我的 tsconfig.app.json 看起来像这样。

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "module": "es2015",
        "types": [
            "googlemaps"
          ]
    },
    "exclude": [
        "test.ts",
        "**/*.spec.ts"
    ]
}

答案 4 :(得分:0)

我对角度7也有同样的问题。 我不需要导入任何内容,只需再次运行npm install @types/googlemaps,如果有任何漏洞,请根据需要运行npm audit fixnpm audit fix --force

这样做之后,一切对我来说都很好...

答案 5 :(得分:0)

我遇到了同样的问题,而我所做的只是删除了这些

import { } from 'googlemaps';
declare var google: any;
从我的component.ts中

并添加     “类型”:[                 “谷歌地图”               ] 在我的tsconfig.app.json中 。 。 现在我的tsconfig.app.json看起来像这样。

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/app",
        "module": "es2015",
        "types": [
            "googlemaps"
          ]
    },
    "exclude": [
        "test.ts",
        "**/*.spec.ts"
    ]
}

它运行良好。

答案 6 :(得分:0)

在IONIC 4中,我通过安装 @ types / google-maps 而不是 @ types / googlemaps

修复了该问题

只需运行

npm install @types/google-maps --save

并使用

在组件中导入 google
import { google } from "google-maps";

答案 7 :(得分:0)

对于Vue js中遇到此问题的任何人,您都可以在脚本部分的顶部添加以下行。确保已安装@types/googlemaps

/// <reference types="@types/googlemaps" />

相关问题