文章详情
【react+typesciprt】react+ts中props.history报错,找不到
发布时间:2022-01-09
沉默小管-技术博客
react+ts中props.history报错,找不到
1103
2
原文地址:
https://www.php-china.com//detail/86
H1
H2
H3
H4
H5
H6
# 问题代码 ```javascript interface CurProps { } interface dataInterface{ data?:any, message:string|number, code:number } interface resInterface{ data:dataInterface } class Login extends Component<CurProps, CurState>{ constructor(props:CurProps){ super(props); } //组件渲染完成后调用 componentDidMount=()=>{ this.props.history.push('/'); } render(){ return ( <>1111</> ) } } export default Login; ``` # 解决流程 1.把CurProps 继承RouteComponentProps 2.页面导出需要用withRouter()包裹 正确代码: ```javascript import {RouteComponentProps, withRouter} from "react-router-dom"; interface CurProps extends RouteComponentProps{ } interface dataInterface{ data?:any, message:string|number, code:number } interface resInterface{ data:dataInterface } class Login extends Component<CurProps, CurState>{ constructor(props:CurProps){ super(props); } //组件渲染完成后调用 componentDidMount=()=>{ this.props.history.push('/'); } render(){ return ( <>1111</> ) } } export default withRouter(Login); ```
上一篇
下一篇
收藏
支持
标签:
最新文章
统计信息
联系站长
加入技术群
日志总数:92篇
网站运行:1424天
累计访客:13007
热门文章
1. PHP基础概念
2. redis:ERR AUTH <password> called without any password configured for the default user解决办法
3. PHP的语言结构之包含文件
4. PHP中public,private,protected的区别
5. PHP魔术方法总结
6. php如何转换json格式中的中文
7. PHP7新特性(常用)
8. PHP7上项目报错A non-numeric value encountered解决办法
9. 运行golang出现invalid memory address or nil pointer dereference错误
10. git常用命令集合