typescript 打字稿错误:找不到命名空间“google”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39909544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:54:50  来源:igfitidea点击:

TypeScript Error: Cannot find namespace 'google'

angulartypescriptangular-clitypescript-typings

提问by mego4iter

I have project angular-cli

我有项目 angular-cli

~root~/src/typings.json

~root~/src/typings.json

{
  "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"
  }
}

~root~/typings/index.d.ts

~root~/typings/index.d.ts

/// <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" />

~root~/src/tsconfig.json

~root~/src/tsconfig.json

{
  "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"
    ]
  }
}

After run ng serveI have error message in console

运行ng serve后 ,控制台中出现错误消息

ERROR in [default] F:~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26

Cannot find namespace 'google'

[默认] F:~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26 中的错误

找不到命名空间“google”

and

ERROR in [default] ~root~\src\app\trip-entry-page\trip-entry-page.component.ts:188:21

Cannot find name 'google'

[默认] ~root~\src\app\trip-entry-page\trip-entry-page.component.ts 中的错误:188:21

找不到名称“谷歌”

~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26

~root~\src\app\ui\google-map\map-marker\map-marker.directive.ts:7:26

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

~root~\src\app\trip-entry-page\trip-entry-page.component.ts:188:21

~root~\src\app\trip-entry-page\trip-entry-page.component.ts:188:21

... 
if (status === google.maps.DirectionsStatus.OK) { 
...

After build app correct working

构建应用程序正常工作后

How me resolve this Error messages?

我如何解决此错误消息?

回答by Stephen Paul

A bit of a late response but I had a similar issue using Angular CLI RC.0.

响应有点晚,但我在使用Angular CLI RC.0 时遇到了类似的问题。

It turned out that I hadn't install and imported the typings, which can be done as follows:

原来是我没有安装导入typings,可以按如下方式进行:

npm install --save-dev @types/googlemaps

import {} from '@types/googlemaps';

回答by Ben Cameron

Try to run the below command in a node prompt...

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

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

回答by MaBbKhawaja

I was facing same problem and what I did was, I just removed these

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

import { } from 'googlemaps';
declare var google: any;

from my component.ts and add "types": [ "googlemaps" ] in my tsconfig.app.json . . Now my tsconfig.app.json looks like this.

从我的 component.ts 并在我的 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"
    ]
}

And its working perfectly.

它的工作完美。

回答by Mab Kiani

In IONIC 4 I fixed it by installing @types/google-mapsnot @types/googlemaps

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

just run this

只需运行这个

npm install @types/google-maps --save

and import googlein your component using

并使用在您的组件中导入google

import { google } from "google-maps";

回答by Ahmed Shehatah

I had the same problem with angular 7. I did not need to import anything just run npm install @types/googlemapsagain and if there is any vulnerabilities, run npm audit fixor npm audit fix --forceif needed.

我在 angular 7 上遇到了同样的问题。我不需要导入任何东西,只需npm install @types/googlemaps再次运行,如果有任何漏洞,请运行npm audit fixnpm audit fix --force在需要时运行。

After I did so, everything worked fine for me...

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