1.首先写一个假的地址(q=URLUtils.searchParams&topic=api
)相当于当前的window.location.href
const urlParams = new URL(window.location.href); urlParams.searchParams.has("topic") === true; // true urlParams.searchParams.get("topic") === "api"; // true urlParams.searchParams.getAll("topic"); // ["api"] urlParams.searchParams.get("foo") === ""; // true urlParams.searchParams.append("topic", "webdev"); urlParams.searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev" urlParams.searchParams.set("topic", "More webdev"); urlParams.searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev" urlParams.searchParams.delete("topic"); urlParams.searchParams.toString(); // "q=URLUtils.searchParams"
是不是很好用?参考(https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams#%E7%A4%BA%E4%BE%8B)