获取地址栏参数的API之URLSearchParams

55 阅读1分钟

URLSearchParams是构造函数,可以解析window.location.search这种?xxx=xx&bb=1里面的参数

eg:页面地址为 "http://10.4.171.132:8085?tenant_id=1656553841572974592&aid=wx0b1d3fd82fa70b72&ssoId=7d6ad78d-a4ee-4052-9612-3b224d7736ef"

const search = localtion.search.substr(1) const params = URLSearchParams(search) params.get('tenant_id') params.get('aid') params.get('ssoId')

有时候会发现# window.location.search为空,一般是因为使用hsah导致的, eg:"http://10.4.171.132:8085/#/pages/shopping-cart/index?tenant_id=1656553841572974592&aid=wx0b1d3fd82fa70b72&ssoId=7d6ad78d-a4ee-4052-9612-3b224d7736ef"

处理: let searchParams = new URLSearchParams(window.location.hash.split("?")[1]); searchParams.get('tenant_id') searchParams.get('aid') searchParams.get('ssoId')