【发布时间】:2022-01-08 05:06:23
【问题描述】:
我使用history.push 重定向到我想要的 url,并通过包含的状态将信息传递给下一个路由,如下所示:
const history = useHistory()
history.push({
pathname: '/someurl/',
state: {
foo: bar
}
})
在 JavaScript 中,这完全符合预期。现在我正在将此文件迁移到 TypeScript,我收到以下错误:
Argument of type '{ pathname: string; state: { foo: bar; }; }'
is not assignable to parameter of type 'To'.
Object literal may only specify known properties, and
'state' does not exist in type 'PartialPath'
出于某种原因,他认为我只传递了一个没有状态的路径名,对吧? 有谁知道如何解决这个问题?
【问题讨论】:
-
你的意思是
pushState不是push...? -
嗯,我想应该是
.push至少我在 js 中工作时是这样做的。 -
history对象没有push,你有没有添加它的库? -
是的,我使用
useHistory访问它,并且返回的历史对象没有pushState -
啊!!
useHistory与window.history不同,所以这可以解释一些事情。 react-router 使用的确定类型是什么版本?
标签: javascript reactjs typescript react-router