axios.get('http://192.168.0.103:3000/weather/Hourly?longitude=${coords.longitude}&latitude=${coords.latitude}')
.then(result => {
console.log(result);
dispatch(fetchWeatherDailySucceeded(result.data.weatherInfor))
})
.catch(error => {
dispatch(fetchWeatherDailyFailed());
})
不知何故,该字符串无法识别我的注入(inject),并且发送到服务器的 url 是带有 ${} 的整个字符串
最佳答案
${}
适用于 `` 反引号,不适用于 "
或 '
您需要使用``(反引号)
`http://192.168.0.103:3000/weather/Hourly?longitude=${coords.longitude}&latitude=${coords.latitude}`
或者,如果您使用 '
或 "
,则可以使用字符串连接
'http://192.168.0.103:3000/weather/Hourly?longitude=' coords.longitude + '&latitude=' + coords.latitude
关于javascript - 将变量注入(inject)字符串 $ 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55499491/