gpt4 book ai didi

reactjs - 从 react.js 中的 url 获取 id

转载 作者:行者123 更新时间:2023-12-03 22:49:54 25 4
gpt4 key购买 nike

我只想让 Ajax 请求从 REACT.JS 中的 API 获取数据,但是每个 API 都需要(id),我想从浏览器中的 URL 获取该 id,我该怎么做?这是我的代码:

componentDidMount(){
fetch("http://localhost:8000/personal/1")
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
information: json,
})
})

我通过 Laravel 制作了 API,并且上面这段代码的一切都很好,但正如我所说,我想从浏览器中的 url 获取 id,而不仅仅是像现在这样写,谢谢:)
}

最佳答案

在 React Hooks 中,你应该使用 useParams() .
ex - 这是你的网址 - http://localhost:8000/personal/1 .
所以在这种情况下,使用 const { id } = useParams()现在,如果你这样做 console.log(id) ,它将为您提供网址的确切 ID。在这种情况下,它会给你 1。
useParams() 从 url 中提取 id 并让我们使用它。
谢谢。

关于reactjs - 从 react.js 中的 url 获取 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57444203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com