location.href中的URL缺少searchParams

问题描述 投票:0回答:1

我正在尝试解析当前路线的查询/搜索参数

let url = new URL(location.href);

[当我查看url时,搜索参数在href中。我希望它们位于searchParams中,该位置为空。我的期望落空了吗?

URL:

hash: "#/admin?ac=flags"
host: "localhost:8084"
hostname: "localhost"
href: "http://localhost:8084/#/admin?ac=flags"
origin: "http://localhost:8084"
password: ""
pathname: "/"
port: "8084"
protocol: "http:"
search: ""
searchParams: URLSearchParams { }

javascript typescript url query-parameters location-href
1个回答
0
投票

不,您的期望是正确的。实际上,searchParams属性返回一个对象URLSearchParams,并且它不是空的。

let urlparams =(new URL(location.href))。searchParams;现在,urlparams包含对象,您可以使用get方法:urlparams.get(“ ac”)

© www.soinside.com 2019 - 2024. All rights reserved.