你好,我在typescript中使用useLocation hooks会报错:
const location = useLocation()
console.log(location.query); // 类型“Location<PoorMansUnknown>”上不存在属性“query”。ts(2339)
另外如果这个问题解决了,读取location.query.id应该也会报错:query上没有id这个属性,应该怎么处理呢?
本来就没有,这个不是 BOM/DOM 对象。see https://github.com/ReactTraining/react-router/issues/7446
对应的文档 API 是:https://reactrouter.com/web/api/location
{
key: 'ac3df4', // not with HashHistory!
pathname: '/somewhere',
search: '?some=search-string',
hash: '#howdy',
state: {
[userDefined]: true
}
}
@sorrycc 我刚才扫了一下 umi 的源码,这个好像确实是个 bug。
umi 导出的 History 和 Location 确实是 DOM 的,从 history-with-query 导出的。但是 useLocation()、useHistory() 是直接从 react-router-dom 导出的。
+1,目前用 history。
useHistory 类型没有query
Most helpful comment
@sorrycc 我刚才扫了一下 umi 的源码,这个好像确实是个 bug。
umi 导出的
History和Location确实是 DOM 的,从 history-with-query 导出的。但是useLocation()、useHistory()是直接从 react-router-dom 导出的。