我要搜索 params
并添加 params
到 URL,但每次都得到空数组。
示例网址 - http://localhost:3000/buildings?search_query=test&page=2
constructor(props) {
super(props);
const params: URLSearchParams = new URLSearchParams(this.props.location.search);
console.log(params); //Getting empty object {}
}
最佳答案
尝试
console.log(params.get("page"));
它应该返回“2”。URLSearchParams 在控制台中显示为一个空对象,并且不显示所有值。
它确实允许您调用 get 函数。
否则请确保 this.props.location.search 确实具有有效的功能。
new URL("http://localhost:3000/buildings?search_query=test&page=2").search
例如,确实返回一个有效值。
关于javascript - URLSearchParams 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63707870/