【问题标题】:TypeError: Cannot read property 'defaultView' of undefined - AngularTypeError:无法读取未定义的属性“defaultView” - Angular
【发布时间】:2019-06-26 05:05:29
【问题描述】:

我遇到了一个运行时错误,它在我的 Angular 应用程序中为我提供了 defaultView 的堆栈跟踪,它阻止我继续我的项目。


错误: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'defaultView' of undefined TypeError: Cannot read property 'defaultView' of undefined

我已经用谷歌搜索了这个,但没有一个问题有角度。所以我认为这是第一个关于 defaultView 的带角度的。

我有一些代码可以获取地址并将标记输出到地图上。我收到了来自https://developers.google.com/maps/documentation/javascript/geocoding的错误


Stackblitz 中的代码https://stackblitz.com/edit/angular-gib2mq

根文件夹中的

index.d.ts 使导入语句起作用:import { } from 'googlemaps';

declare module "googlemaps";

我还通过npm install --save @types/googlemaps 安装了@types/googlemaps


我希望错误消失,但每次运行我的应用程序时都会发生。

谢谢!

【问题讨论】:

  • 尝试 import * as googlemaps from 'googlemaps'; 代替您的导入语句
  • 上面写着'googlemaps' is declared but its value is never read 编辑:现在我使用它时它就崩溃了:找不到模块。
  • 这是一个tslint 错误,您的实际错误是否已解决?
  • 不,没有解决。编辑:我需要这个吗? npmjs.com/package/tslint-angular
  • 当我尝试使用我的 API 密钥时,您的请求没有被触发

标签: angular google-maps geolocation angular-google-maps


【解决方案1】:

看起来你对如何使用 @agm 包有很大的误解。没关系!一些笔记:

  • 您不需要 this.map 或 latlng 类型。您只需将数字传递给组件
  • 需要重新定位时在地图上调用 triggerResize(true)
  • 您不需要在配置中显式导入库...不确定为什么会这样。只需确保您使用的 API 密钥已启用它们即可
  • 最后,您需要给地图一个静态高度,或者它的父级需要一个(css 原因,我不会深入探讨)。您始终可以使用打字稿设置高度。

工作堆栈闪电战(减去 API 密钥):https://stackblitz.com/edit/angular-yebacm

祝你好运!

编辑:你也不需要declare module "googlemaps";

【讨论】:

  • triggerResize(true) 如何知道以哪个坐标为中心?
  • 它基于 agm-map 组件上的 lat 和 long 输入
  • 在您的情况下,它将基于 this.latthis.lng
猜你喜欢
  • 1970-01-01
  • 2014-11-29
  • 2018-10-29
  • 2021-07-21
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多