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
添加回答
舉報