5

が定義されていない私はAngular-cliを使用して、私の角度2のプロジェクトで作業GoogleマップPlaceResultをしようと時間のカップルのために苦労してきました。角度2(角度-CLI):キャッチされないにReferenceError:グーグルが

googlemapsを@typesを使用してインストールし、"types"の設定ファイルtsconfig.jsonに追加する必要がありました。

{ 
    ... 
    "types": [ 
     "google-maps" 
    ] 
    } 
} 

そして、私は単にそれをインポートして、私の角2コンポーネントでgoogle.maps.places.PlaceResultを使用するために成功!

import { ActivatedRoute, Params } from "@angular/router"; 
import { MapsAPILoader } from "angular2-google-maps/core"; 

import PlaceResult = google.maps.places.PlaceResult; 
import GeocoderRequest = google.maps.GeocoderRequest; 
... 

数時間後、私はPlaceResultGeocoderRequestと同じ定義ファイルである、google.maps.Markerで仕事をしていました。だから私は、単に以下のようにそれをインポート:

[Line 12] import PlaceResult = google.maps.places.PlaceResult; 
[Line 13] import GeocoderRequest = google.maps.GeocoderRequest; 
[Line 14] import Marker = google.maps.Marker; 
[Line 15] import LatLng = google.maps.LatLng; 
... 

しかし、私は

Uncaught ReferenceError: google is not defined search.component.ts:14 
at Object.444 (search.component.ts:14) 
at __webpack_require__ (bootstrap 26c2b97…:52) 
at Object.727 (app.config.ts:11) 
at __webpack_require__ (bootstrap 26c2b97…:52) 
at Object.602 (src async:7) 
at __webpack_require__ (bootstrap 26c2b97…:52) 
at Object.1258 (.*$:7) 
at __webpack_require__ (bootstrap 26c2b97…:52) 
at webpackJsonpCallback (bootstrap 26c2b97…:23) 
at main.bundle.js:1  

を言って、実行時に予期しないエラーが発生しましたWebPACKのがライン私のコンポーネントの14時にこのエラーがスローされますのでご注意ください。以前の行(同じ "google"を使用している)がうまくいったことを意味します(私が間違っていれば修正します)。

Am'Iには何かが欠けていますか?


は、私が使用します。

  • 角度:2.4
  • 角度-CLI:1.0.0-beta.24
  • typescriptです:2.0.10
  • angular2-google-maps:0.17.0

答えて

1

Iは、以下の設定を有することにより、私の問題を解決:

"types": [ 
     ... 
     "googlemaps" 
    ] 

3- tsconfig.json

1- package.json

"dependencies": { 
    ... 
    "googlemaps": "^1.12.0", 
    ... 
    } 

2- を私の index.html 01にgoogleのapiスクリプトを追加してください成分中

<head> 
    ... 
</head> 
<body> 
    <app-root>Loading...</app-root> 

    <script src="https://maps.googleapis.com/maps/api/js?key=KEY&libraries=places&language=fr" async defer></script> 
</body> 
</html> 

4-、

declare var google: any; 

@Component({ 
    ... 
}) 
export class SearchComponent implements OnInit, AfterViewInit { 

    // Google Maps 
    bounds: google.maps.LatLngBounds; 
    markers: google.maps.Marker[]; 
    infoWindow: google.maps.InfoWindow; 

} 
以下のようにそれを使用
関連する問題