[react] this.props 中找不到 history

问题
在用React开发时 要通过this.props.history.replace('路径')跳转 ,在this.props却找不到.history 导致无法跳转 。
原因
由于 React 中,只有在 <Route> 包裹的组件中,才能在 this.props 属性中找到 history
解决方案
第一种 : 直接用<Route> 进行包裹。(直接用,不说了)
第二种:不用用React 进行包裹。(以下两种方式)

1、这种方式会出现 Support for the experimental syntax ‘decorators-legacy’ isn’t currently enabled 的报错

import { withRouter } from 'react-router-dom'
@withRouter
class ...

2、是在第一种方式进行修改,解决报错问题;

import { withRouter } from 'react-router-dom'
export default withRouter(组件)

顺便记录下react中页面跳转的方式

  1. this.props.history.replace('路径')this.props.history.path('路径')
  2. <Redirect to='路径' />
  3. window.location.href = '路径';
  4. window.open('about:blank').location.href = '路径';
    :不同的方式有不同的使用场景
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值