简体   繁体   English

Angular 6 应用程序找不到命名空间“google”

[英]Angular 6 application cannot find namespace 'google'

This question has appeared similarly in many places where the solution is to simply add这个问题在很多地方也有类似的出现,解决方法是简单地添加

import { } from '@types/googlemaps';

which worked as a solution in past versions of angular.它在过去版本的 angular 中作为解决方案起作用。 The issue appeared now that I am using Angular 6+现在我使用的是 Angular 6+,这个问题就出现了

TS2304: Cannot find name 'google'.
TS2503: Cannot find namespace 'google'.

There are numerous errors like this anywhere I use google maps types.在我使用谷歌地图类型的任何地方都有很多这样的错误。 For example:例如:

let place: google.maps.places.PlaceResult = autocomplete.getPlace();

I can quickfix the issue by inserting // @ts-ignore above all lines that use google maps, but I am much more interested in a true fix.我可以通过在所有使用谷歌地图的行上方插入// @ts-ignore修复问题,但我对真正的修复更感兴趣。 But the fact this works makes me feel it's a tsconfig issue which I am not super confident about.但是这个工作的事实让我觉得这是一个我不太自信的 tsconfig 问题。

I can confirm that googlemaps is installed inside node_modules/@types, but I am not sure about the tsconfig我可以确认 googlemaps 安装在 node_modules/@types 中,但我不确定 tsconfig

ts.config配置文件

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "typeRoots": [
      "node_modules/@types",
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

I created a Stackblitz Example which throws a Reference Error if you view the console.我创建了一个Stackblitz 示例,如果您查看控制台,它会抛出一个参考错误。 I don't know what to try next.我不知道接下来要尝试什么。

So I came across the problem earlier on GitHub and this worked for me:所以我早些时候在 GitHub 上遇到了这个问题,这对我有用:

  • npm install --save-dev @types/googlemaps

  • Adding to all my tsconfig*.json : types: [ "googlemaps"]添加到我所有的tsconfig*.jsontypes: [ "googlemaps"]

  • Adding at the top of my file: declare const google: any ;在我的文件顶部添加: declare const google: any ;

  • Adding at the end of the body of my index.html :index.html正文的末尾添加:

    <script async defer type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=****"> </script>

Try it out and tell me whether it works.试试看,然后告诉我它是否有效。

The tsconfig.json file that really needs to be updated is in src/tsconfig.app.json.真正需要更新的tsconfig.json文件在src/tsconfig.app.json。

That files overrides the types property of compilerOptions of the tsconfig.json file in the root directory with an empty array so you must add the types definition for googlemaps there.该文件使用空数组覆盖根目录中 tsconfig.json 文件的 compilerOptions 的 types 属性,因此您必须在那里添加 googlemaps 的类型定义。

For example:例如:

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

Adding it to the types array of my tsconfig compiler options has never worked.将它添加到我的 tsconfig 编译器选项的 types 数组中从未奏效。 But if you have the @types/googlemaps installed, you can reference this at the top of your .ts file using:但是如果你安装了@types/googlemaps,你可以在你的 .ts 文件的顶部使用:

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

cf.参见Triple Slash Directives 三重斜线指令

I fixed it by installing @types/google-maps not @types/googlemaps我通过安装@types/google-maps而不是@types/googlemaps 来修复它

just run this只需运行这个

npm install @types/google-maps --save

and import google in your component using并使用在您的组件中导入 google

import { google } from "google-maps";

我发现了一些东西,如果你使用 AGM 你可以修复它导入:

import {} from '@agm/core/services/google-maps-types';

Recently I came across this issue and it occurs due to many reasons I mention all the possible solution here:最近我遇到了这个问题,它发生的原因有很多,我在这里提到了所有可能的解决方案:

Solution 1:解决方案1:

i) If you haven't enabled your google Places API to go through this link link to enable it. i) 如果您尚未启用您的 google Places API,请通过此链接链接启用它。

ii) install @types/googlemaps in your project using npm i @types/googlemaps --save-dev for more details click here ii) 使用npm i @types/googlemaps --save-dev在您的项目中安装 @types/googlemaps 了解更多详情,请点击此处

iii) Add "types": ["googlemaps"] this to your tsconfig.json most of the case file name is tsconfig.app.json if you are curious about types check out this answer on StackOverflow. iii) 添加"types": ["googlemaps"]到您的 tsconfig.json 大部分案例文件名是 tsconfig.app.json 如果您对类型感到好奇,请查看 StackOverflow 上的这个答案

iv) restart your server iv) 重新启动您的服务器

In most cases, there is no need for declaring any google variable and the above solution works in my case also.在大多数情况下,不需要声明任何 google 变量,上述解决方案也适用于我的情况。

Solution 2:解决方案2:

if solution 1 does not work include this <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script> in your index.html and put your api key in this link.如果解决方案 1 不起作用,请将此<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>在您的索引中.html 并将您的 api 密钥放在此链接中。

in one of my project solution 2 works.在我的项目解决方案 2 之一中。

Solution 3:解决方案3:

and if solution 1 and solution 2 do not work write this code in your ts file declare const google: any;如果解决方案 1 和解决方案 2 不起作用,请在您的 ts 文件中编写此代码declare const google: any;

I hope this will be helpful.我希望这会有所帮助。

Thanks谢谢

For anyone using AGM running into this error:对于任何使用 AGM 遇到此错误的人:

Cannot find name 'google'.

Running this worked for me:运行这个对我有用:

npm install @types/google-maps --save

I was using AGM and if well this problem exist was the product of other problem.我正在使用 AGM,如果这个问题存在,则是其他问题的产物。 That is here:https://github.com/DefinitelyTyped/DefinitelyTyped/issues/48574 And the solution was: "I found a temporary solution, install an earlier version of @types/googlemaps in the package.json, I added @types/googlemaps: "3.39.12" to my devDependencies, now the production build works !"那在这里:https ://github.com/DefinitelyTyped/DefinitelyTyped/issues/48574而解决方案是:“我找到了一个临时解决方案,在 package.json 中安装早期版本的@types/googlemaps,我添加了@types /googlemaps: "3.39.12" 给我的 devDependencies,现在生产构建工作了! If you will be try this , first run: npm uninstall @types/google-maps如果您要尝试此操作,请先运行: npm uninstall @types/google-maps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM