[英]I can't programmatically change route in react-router
创建模型后,我试图以编程方式重定向到新的路线/组件。 <Link>
组件按预期方式工作。 我正在使用MobX。 在我使用Redux之前,这是可行的。 当我移至MobX时,它停止工作。 单击<Link>
组件将按预期工作。
渲染组件:
<Router history={locationStore.history}>
<div>
<SideBar />
<div className={mergeClass('app-content text-center', { expanded: layoutStore.sidebarOpen })}>
<NavBar />
<div className='page-wrapper'>
<Route exact path='/' component={Home} key='home' />,
<Route exact path='/stuff/:id/edit' component={StuffEdit} key='stuff.edit' />,
</div>
</div>
</div>
</Router>
位置商店:
import { observable } from 'mobx'
import createBrowserHistory from 'history/createBrowserHistory'
const history = createBrowserHistory()
class LocationStore {
@observable history = history
goTo (url) {
this.history.push(url)
}
}
export default new LocationStore()
这是我尝试更改路线的地方:
return Api.post('/stuff', formData).then((response) => {
stuffStore.addStuff(response.data)
locationStore.goTo(`/stuff/${response.data.stuff.id}/edit`)
})
URL在浏览器中更改,但是组件保留在原始页面上。 单击生成的Link
组件到新模型,按预期方式工作(无需刷新)。
反应版本:15.6.1
React Router版本:4.1.2
Mobx版本:3.2.2
Mobx反应版本:4.2.2
也许你可以试试看
<Route path="/" component={App}> <Route path='path0' component={UserManage}></Route> <Route path='path1'> <Route path='path11' component={component11}></Route> <Route path='path12' component={component12}></Route> </Route> </Route> in component11, you can try this way to change the route: this.props.router.push( '/path1/path12' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.