作爲在瀏覽器中輸入http://localhost:3000/ui/?goto=/ui/entity/e2
的示例,我想轉到實體組件e2。如何根據URL參數轉到React的特定頁面
這是我的路由器:
<Route path="/ui/" component={App}>
<IndexRoute component={EntitiesPage} />
<Route component={Entity} path="entity/:id" />
<Route component={NotFound} path="*" />
</Route>
這是應用程序組件:
import React from 'react'
const App = React.createClass({
render() {
let gotoUrl = this.props.location.query.goto;
if (gotoUrl) {
// go here: gotoUrl;
} else {
return (
<div className="App">
{this.props.children}
</div>
)
}
}
})
export default App
this.context是空的。 this.props有:
- 歷史
- 位置
- 路線
- routeParams(空)
- 路線
更新: 我已經結束了使用此:
import React from 'react'
import { withRouter } from 'react-router'
const App = React.createClass({
componentWillMount() {
let gotoUrl = this.props.location.query.goto;
if (gotoUrl) {
this.props.router.replace(gotoUrl);
}
},
render() {
return (
<div className="App">
{this.props.children}
</div>
);
}
})
export default withRouter(App)