【问题标题】:Google places error in React 16 after upgrade from 15.x从 15.x 升级后,Google 在 React 16 中出现错误
【发布时间】:2018-05-29 06:08:30
【问题描述】:

我刚刚将我的 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>

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

【问题讨论】:

    标签: reactjs google-places-api googleplacesautocomplete


    【解决方案1】:

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

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

    【讨论】:

    • 是的,这解决了问题。谢谢!
    猜你喜欢
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2016-05-12
    • 2018-12-27
    • 1970-01-01
    相关资源
    最近更新 更多