2016-09-27 132 views
0

在我的阵营路由器 - 终极版重拨应用程序,我们可以用两种URL特定网址:盼着来与路由器

  • /帐户ID
  • /帐户ID /语言

该语言是可选的。
当语言参数未设置时,我们必须强制使用“fr”语言。
react-router有没有可能自动将“/ accountId”改为“/ accountId/fr”?

IndexRoute没有任何路径属性来强制url并且重定向功能不符合我们的需要,我们希望在url中看到“/ accountId/fr”,这是因为一些SEO原因。

我试图在我的LandingCmp的渲染函数中进行调度(push(“/ accountId/fr”)),但它不起作用。

我们使用重拨(@provideHooks)来确保服务器端的一个干净的预渲染。

这里是我当前的路由表:

<Route path='/'> 
    <Route path="error" component={ErrorsCmp} /> 
    <Route path=":accountId"> 
    <Route path=":langue"> 
     <IndexRoute component={LandingCmp} /> 
    </Route> 
    <IndexRoute component={LandingCmp} /> 
    </Route> 
</Route> 

我们用下面的依赖关系: - “反应路由器 - 终极版”: “4.0.5” - “反应路由器”:“2.7。 0“

回答

0

您可以导入browserHistory,然后使用它动态地推送到不同的路由。所以,你可以检查PARAMS,以找出是否有帐户ID之后的任何参数,如果没有,你可以使用:

browserHistory.push("/accountId/fr"); 

并导入browserHistory:

import {browserHistory} from 'react-router'; 
0

您可以push网址使用withRouter包装的componentWillMount生命周期方法,该包装在更新版本的React Router(自版本2.4.0起)中可用。更多信息:https://github.com/ReactTraining/react-router/blob/master/docs/API.md#withroutercomponent-options

结构可能看起来像如下:

import { withRouter } from 'react-router'; 

class App extends React.Component { 
    componentWillMount() { 
    this.props.router.push('/accountId/fr'); 
    } 

    render() { 
    // your content 
    } 
} 

export default withRouter(App);