Reactjs react-google-maps undefined

Dee*_*eee 5 javascript maps google-maps google-maps-api-3 reactjs

我正在使用react-google-maps包https://github.com/tomchentw/react-google-mapshttps://www.npmjs.com/package/react-google-maps.我有一个错误说:

./src/App.js Line 31:  'google' is not defined  no-undef  Line 32:  'google' is not defined  no-undef  Line 37:  'google' is not defined  no-undef Line 42:  'google' is not defined  no-undef Line 44:  'google' is not defined  no-undef
Run Code Online (Sandbox Code Playgroud)

并且我在错误中的行:

 state = {
    origin: new google.maps.LatLng(41.8507300, -87.6512600),
    destination: new google.maps.LatLng(41.8525800, -87.6514100),
    directions: null,
}

componentDidMount() {
    const DirectionsService = new google.maps.DirectionsService();

    DirectionsService.route({
        origin: this.state.origin,
        destination: this.state.destination,
        travelMode: google.maps.TravelMode.DRIVING,
    }, (result, status) => {
        if (status === google.maps.DirectionsStatus.OK) {
            this.setState({
                directions: result,
            });
        } else {
            console.error(`error fetching directions ${result}`);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

所有'谷歌'的事情都是错误的.

Dee*_*eee 6

答案是我没有将此行包含/* global google */在我的文件顶部.

  • 请给我一些详细的答案吗? (2认同)