与打字稿反应:类型“历史记录”上不存在属性“推送” [英] React with Typescript: Property 'push' does not exist on type 'History'

查看:302
本文介绍了与打字稿反应:类型“历史记录”上不存在属性“推送”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过推入历史对象来离开视图。但是,当我尝试将一条路线插入其中时,会收到一条错误消息:

I'm trying to navigate away from a view by pushing into the history object. However when I try to push a route into it, I get an error message:

属性 push在历史记录类型上不存在。

Property 'push' does not exist on type 'History'.

  render(){
    return (
        <div className="loginWrapper">
    withRouter(({history}) => (<button onClick={()=>{history.push('/home')}} className="btn btn-primary">Pieteikties</button>))
  </div>
    )  
}

该如何解决?

编辑:

我也尝试过此操作:

logIn(){
  this.props.history.push('/new-location')
}

具有这样的组件:

 render(){
    return (
        <div className="loginWrapper">
<button onClick={this.logIn} className="btn btn-primary">Pieteikties</button>
</div>
    )  
}

没用。

推荐答案

您在哪里定义历史记录?有一个全局 window.history 这是一个网络标准。如果您想使用反应路由器的历史记录,它会作为道具传递。尝试使用 props.history.push()

Where did you define history here? There is a global window.history which is a web standard. If you want to use the react-router history, it's passed as a prop. Try props.history.push() instead.

这篇关于与打字稿反应:类型“历史记录”上不存在属性“推送”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆