在 React 中正常来说只有在 包裹的组件中才能在 this.props 属性中找到 history 对象
但有些情况下我们就是希望在一个没被 包裹的组件中用到 history 对象,比如:异步加载数据后跳转
网上有些方法提到可以在 class 前加上 @withRouter 的修饰
import { withRouter
} from 'react-router-dom'
@withRouter
class ...
这种做法有时候会出现 Support for the experimental syntax ‘decorators-legacy’ isn’t currently enabled 的报错可以试着改成
export default withRouter(YourComponent
)
或者(如果你还需要 connect)
export default withRouter(connect(mapStateToProps
)(YourComponent
))