首页 > 解决方案 > TypeError:无法读取未定义的属性“defaultView” - 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 语句起作用:import { } from 'googlemaps';

declare module "googlemaps";

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


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

谢谢!

标签: angulargoogle-mapsgeolocationangular-google-maps

解决方案


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

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

工作 stackblitz(减去 API 密钥):https ://stackblitz.com/edit/angular-yebacm

祝你好运!

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


推荐阅读