简体   繁体   中英

How to import namespace when using third party lib with angular-cli

According to Angluar-cli documentation here , I succeed to import any third party library and then to use typings ( dt and custom). Youhouou !

But for Google types, I have to prefix all my types by the google.maps namespace :

let geocoder = new google.maps.Geocoder();

What can I do to use short names for types like this :

let geocoder = new Geocoder();

Thanks for any suggestion.

Detail of my Google Maps import are here : Getting started - How to use Google maps api with angular-cli

==== EDIT ====

The @Nikky answer show me the mistake with the google namespace : It will be available at runtime only after the google maps script will be loaded.

So the problem is located in all the constructor() and ngOnInit() that use the google.map types :

  constructor() {
    this.load();                     //will begin goooglemaps script loading
    this.onReady().then(() => {      //subscribtion to script loaded event
      this.geocoder = new google.maps.Geocoder();//'google is not defined' at runtime
    });
  }

You can import the namespace and assign a name to it

import Geocoder = google.maps.Geocoder;

And then use it as if you were using native Geocoder API

let geocoder = new Geocoder();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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