javascript - 在 React 中向 URL 添加变量
问题描述
我需要在 URL 中传递城市名称。我已经声明了变量城市,我在 URL 中传递它,但它没有读取变量我尝试了很多方法,但它们不起作用。
const Weather=({city})=>{
const [weather,setWeather]=useState('')
useEffect(() => {
axios
.get(`http://api.weatherstack.com/current?access_key=58hgf1d8d36bf336f347d6cc9fbc7&query=${city}`)
.then(response => {
setWeather(response.data)
})
}, [])
// ...
解决方案
您使用的模板文字语法不正确。你需要:
.get(`http://api.weatherstack.com/current?access_key=Your_access_Key&query=${city}`)
但看起来您在此请求中缺少访问密钥。
推荐阅读
- intellij-idea - 无法在没有扩展错误的情况下执行 Clojure 宏
- c# - 如何使用控制台应用程序使用 C# .NET Core (windows + linux) 创建 Facebook RTSP 流?
- c++ - 初始化类类型时直接初始化会出错
- erlang - 具有非默认 EPMD 端口的 Erlang 节点不再响应 ping
- google-apps-script - 优化 .gs 搜索和替换脚本?
- sql - 如何从datetime sql中提取时间
- linux - 在字符串匹配后查找哪些文件具有重复字符
- search - 自定义 docusaurus 搜索栏 v2
- flutter - 如何使用 Flutter Riverpod 充当 ChangeNotifierProxy Provider?
- html - 如何在圆圈上方添加文字?