我正在尝试使用Url对象读取页面Url。
let url:URL = new URL(window.location)
这返回一个错误Argument of type 'Location' is not assignable to parameter of type 'string'.
let url:Location = new URL(window.location)
这会返回Type 'URL' is missing the following properties from type 'Location': ancestorOrigins, assign, reload, replace
那么URL对象的类型是什么呢?
感谢
错误与let url
的类型无关。问题是,根据TypeScript,new URL()
需要一个字符串,但您给它一个Location
对象。这将在纯JavaScript中工作,因为Location将被转换为字符串。
为了满足编译器的要求,你需要更明确地向构造函数传递一个字符串:
let url: URL = new URL(window.location.href);
游乐场链接