2017-09-08 15 views
1

ここからJS newbieに反応してください。 「Google」が定義されていません:React JSでGoogle Maps APIを追加してください

は、私はこのように、私の index.htmlファイルにGoogleマップAPIスクリプトを追加しました:

<!doctype html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
    <meta name="theme-color" content="#000000"> 

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json"> 
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css"> 

    </head> 
    <body> 

    <div id="root"></div> 

    <script src="https://maps.googleapis.com/maps/api/js?key=MY-API-IS-HERE"></script> 
    </body> 
</html> 

その後、私はので、私はそれがコンポーネントを使用することができますnpmgoogle-maps-reactをインストールしました。私はgoogle.maps.Geocoderを使用しようとすると、私が得る、しかし

import React, { Component } from 'react'; 
import GoogleMapReact from 'google-map-react'; 

class BaseMap extends Component { 
    constructor() { 
     super(); 
     this.state = { 
      center: {lat: 42, lng: 14}, 
      zoom: 10 
     } 
    }; 

    render() { 
     let location = { 
      center: '', 
      zoom: '' 
     } 

     return (
      <GoogleMapReact> 
      </GoogleMapReact> 
     ); 
    } 

    myMethod(){ 
     let geocoder = new google.maps.Geocoder(); 
    } 
} 

export default BaseMap; 

::私が知っているように

Line 30: 'google' is not defined no-undef 

は、googleはグローバル、ウィンドウオブジェクトでなければなりません私ので、私はこのような私のコンポーネントでこのライブラリをインポートhtmlファイルにGoogle Maps APIスクリプトが含まれています。ただし、定義されていません。

アイデア?事前にTnx!

const google = window.google 

ここに答えた:

ソリューションは、暗黙のうちにこのように、クラス以上の変数にgoogleを定義することでした:

答えて

関連する問題