-1
我声明变量名称与一个空的数组值出功能,然后用它在函数内分配返回的值,然后我想用它在这个函数以外,但外面该变量的功能保持为空。 请解释我做错了什么。我不能使用变量功能
handleUpdateInputBusinessName = (searchText) => {
var names = [];
var displaySuggestions = function(predictions, status) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
alert(status);
return;
}
predictions.forEach(function(prediction) {
let name = prediction.description;
names.push(name);
});
console.log('names_in:', names); // valid predictions from Google Maps Service
return names;
};
console.log('names_out:', names); // []
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({ input: searchText, types: '(cities)'
}, displaySuggestions);
您是否考虑过使用'state'而不是? – Nocebo
remove var names = [];这条线内功能。 –