Compiler v2
Compiler v2

Reputation: 3605

TypeError: Cannot read property 'defaultView' of undefined - Angular

I am having a runtime error where it gives me the stack trace of defaultView in my angular app, and it prevents me from moving on in my project.


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

I have googled this, but none of the questions have angular in them. So I think this is the first one about defaultView with angular.

I have some code that takes an address and outputs a marker on to the map. I got the error from https://developers.google.com/maps/documentation/javascript/geocoding


Code in Stackblitz: https://stackblitz.com/edit/angular-gib2mq.

index.d.ts in the root folder makes the import statement work: import { } from 'googlemaps';

declare module "googlemaps";

I also have @types/googlemaps installed through npm install --save @types/googlemaps


I want to the error to go away, but it happens every time I run my app.

Thanks!

Upvotes: 0

Views: 3251

Answers (1)

dockleryxk
dockleryxk

Reputation: 407

Looks like you have a pretty big misunderstanding of how to use the @agm package. That's okay! Some notes:

  • you don't need this.map or the latlng types. You just pass numbers the to components
  • call triggerResize(true) on the map when you need to recenter
  • you don't need to explicitly import the libraries in the config...not sure why that is actually. Just make sure the API key you are using has them enabled
  • finally, you need to give the map a static height, or its parent needs one (css reasons that I won't get into). You can always set the height with typescript.

Working stackblitz (minus the API key): https://stackblitz.com/edit/angular-yebacm

Good luck!

edit: you also don't need declare module "googlemaps";

Upvotes: 1

Related Questions