@@ -20,7 +20,11 @@ import {
20
20
OnInit ,
21
21
Output ,
22
22
ViewEncapsulation ,
23
+ Optional ,
24
+ Inject ,
25
+ PLATFORM_ID ,
23
26
} from '@angular/core' ;
27
+ import { isPlatformBrowser } from '@angular/common' ;
24
28
import { BehaviorSubject , combineLatest , Observable , Subject } from 'rxjs' ;
25
29
import { map , shareReplay , take , takeUntil } from 'rxjs/operators' ;
26
30
@@ -48,12 +52,6 @@ export const DEFAULT_HEIGHT = '500px';
48
52
/** Arbitrary default width for the map element */
49
53
export const DEFAULT_WIDTH = '500px' ;
50
54
51
- /**
52
- * Whether we're currently rendering inside a browser. Equivalent of `Platform.isBrowser`,
53
- * but copied over here so we don't have to add another dependency.
54
- */
55
- const isBrowser = typeof window === 'object' && ! ! window ;
56
-
57
55
/**
58
56
* Angular component that renders a Google Map via the Google Maps JavaScript
59
57
* API.
@@ -194,6 +192,8 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
194
192
private _mapEl : HTMLElement ;
195
193
_googleMap ! : UpdatedGoogleMap ;
196
194
195
+ /** Whether we're currently rendering inside a browser. */
196
+ private _isBrowser : boolean ;
197
197
private _googleMapChanges ! : Observable < google . maps . Map > ;
198
198
199
199
private _listeners : google . maps . MapsEventListener [ ] = [ ] ;
@@ -205,8 +205,19 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
205
205
206
206
private readonly _destroy = new Subject < void > ( ) ;
207
207
208
- constructor ( private readonly _elementRef : ElementRef ) {
209
- if ( isBrowser ) {
208
+ constructor (
209
+ private readonly _elementRef : ElementRef ,
210
+ /**
211
+ * @deprecated `platformId` parameter to become required.
212
+ * @breaking -change 10.0.0
213
+ */
214
+ @Optional ( ) @Inject ( PLATFORM_ID ) platformId ?: Object ) {
215
+
216
+ // @breaking -change 10.0.0 Remove null check for `platformId`.
217
+ this . _isBrowser =
218
+ platformId ? isPlatformBrowser ( platformId ) : typeof window === 'object' && ! ! window ;
219
+
220
+ if ( this . _isBrowser ) {
210
221
const googleMapsWindow : GoogleMapsWindow = window ;
211
222
if ( ! googleMapsWindow . google ) {
212
223
throw Error (
@@ -224,7 +235,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
224
235
225
236
ngOnInit ( ) {
226
237
// It should be a noop during server-side rendering.
227
- if ( isBrowser ) {
238
+ if ( this . _isBrowser ) {
228
239
this . _mapEl = this . _elementRef . nativeElement . querySelector ( '.map-container' ) ! ;
229
240
this . _setSize ( ) ;
230
241
0 commit comments