1 回答

TA贡献1998条经验 获得超6个赞
props.location.search.split("=")
on"?quantity=1?mixOne=Grape"
会返回,[ '?quantity', '1?mixOne', 'Grape' ]
因为 next=
直到 after 之后才会返回mixOne
。
这里有一些不同的修复。
您的查询字符串无效 - a
?
表示查询字符串的开头。&
应使用& 字符分隔单独的参数。它应该看起来像这样:?quantity=1&mixOne=Grape
如果您遵循此处的标准,则可以将其拆分为两种方式:by
=
和 then by&
以获得不同的参数。然而,还有一个更简单的方法。使用新的URLSearchParams API,您可以以可预测的方式解析您的参数:
// Use the constructor with your `props.location.search`
const queryParams = new URLSearchParams(props.location.search);
// Use the getters to grab a specific value
const quantity = queryParams.get("quantity");
// Ensure it's a number for safety
const quantityNum = Number(quantity);
// ... the rest of your code here
添加回答
举报