[英]React Error : (function) is not defined at HTMLButtonElement.onclick
[英]React onclick function not defined
问题是从一个位置到另一个位置的 Google Maps API 显示方向返回错误函数 displayDirections() 未定义。
完整代码在这里,一直在纠结引号和单引号等,但不要认为这是问题所在。 列出了此函数,我的所有其他函数都可以在没有问题的情况下调用。 当我输入目的地,然后出现infoWindows时,我选择了查看路线按钮,但我仍然收到未定义displayDirections的错误,我什至将此功能在App.js中全局移动,但我仍然无法弄清楚为什么这个函数是
只需剪下代码,如果缺少某些内容,则可能在完整代码中;
function displayDirections(origin) {
hideListings()
let directionsService = new window.google.maps.DirectionsService()
let destinationAddress = document.getElementById('search-within-time-text').value
let mode = document.getElementById('mode').value
directionsService.route({
origin: origin,
destination: destinationAddress,
travelMode: window.google.maps.TravelMode[mode]
}, function(response, status) {
if (status === window.google.maps.DirectionsStatus.OK) {
let directionsDisplay = new window.google.maps.DirectionsRenderer({
map: map,
directions: response,
draggable: true,
polylineOptions: {
strokeColor: 'green'
}
}) } else {window.alert('Directions request failed due to ' + status)
}
})
}
let infowindow = new window.google.maps.InfoWindow({ content: durationText + ' away,
' + distanceText + '<div><input id="route" type="button" value="View Route" onclick
=' + '{displayDirections("origins[i]")}></input></div>'})
这应该是模板文字吗:
onclick=displayDirections(`${origins[i]}`)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.