0

我刚刚将我的 React 应用程序从 15.x 升级到 React 16.0,现在出现以下错误。代码本身并没有改变,只是它现在是一个 React 16.0 应用程序。

'google' 未定义 no-undef

我在创建的 util 方法中使用自动完成功能,如下所示:

const map = new google.maps.Map(document.createElement('div'));
const googlePlacesAutocomplete = new google.maps.places.AutocompleteService();
const googlePlacesService = new google.maps.places.PlacesService(map);

export const googlePlacesAutoComplete = (keyword, callback) => {

    googlePlacesAutocomplete.getQueryPredictions({input: keyword}, (predictions, status) => {

        if(status !== google.maps.places.PlacesServiceStatus.OK) return callback(null);
        return callback(predictions);
    });
}

我只是从静态 HTML 页面指向 Google Places 库作为我的 React 应用程序的入口点。

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&key=my-api-key"></script>

知道有什么问题吗?有什么建议可以解决这个问题吗?

4

1 回答 1

0

你试过'window.google'吗?'google' 是全局变量,建议不要按原样使用全局变量。

此外,如果可以,请尝试将“google”添加为模块,以确保它在使用前已加载。并且可能避免使用全局变量或任何可能覆盖其值的东西。

于 2018-05-30T00:52:55.267 回答