跪求!react-router4怎么在非组件内进行路由跳转

在axios的请求拦截里需要做路由跳转,不在组件内部尝试使用importcreateHistoryfrom'history/createBrowserHistory'createHistory().push('/path')这个方式,url会变化,页面不会更新
心有法竹
浏览 776回答 2
2回答

慕雪6442864

使用connected-react-redux,将store传入进去,直接import{push}from'connected-redux-router'store.dispatch(push('/path'))直接使用使用window.location='/xxx'跳转传入history在创建Router对象传入的history对象

三国纷争

我也遇到这个问题了,我是这么解决的,在根组件(App.jsx)中将history挂到windows上,这样在任意地方都能通过windows获取history对象,然后就能正常跳转了。//App.jsxconstructor(props){super(props);this.state={};window.reactHistory=this.props.history;}//任意地方使用window.reactHistory.push("/")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript