reactjs - 404页面 - 语言参数缺少处理问题

时间:2017-05-19 14:09:00

标签: javascript reactjs routes

我是reactjs的新手 - 在使用语言切换的网站上工作我正在尝试创建一个用作404页面的错误组件 - 但也试图了解如何配置/自动切换网站以使用默认语言如果未定义参数或将错误路径设置为仍然监听lang参数。

router.js目前看起来像这样

    <Switch>
                    <Route exact path='/' render={() => (<Redirect to='/de/dienstleistungen/geistiges-eigentum' />)} />
                    <Route path='/:langURL/services' component={Services} />
                    <Route path='/:langURL/dienstleistungen' component={Services} />
                    <Route path='/services' component={Services} />
                    <Route path='/:langURL/how-it-works' component={HowItWorks} />
                    <Route path='/:langURL/anleitung' component={HowItWorks} />
                    <Route path='/:langURL/features' component={Features} />
                    <Route path='/:langURL/funktionen' component={Features} />
                    <Route path='/:langURL/beliebte-projekte' component={BundleDetails} />
                    <Route path='/:langURL/popular-projects' component={BundleDetails} />
                    <Route component={Error} />
</Switch>

1 个答案:

答案 0 :(得分:0)

我认为您说问题是您希望用户无法在网址中提供语言参数?现在,如果他们不提供它们,他们会被抛入Error组件?您可以使用:langURL?

使该参数成为可选参数
<Switch>
    <Route exact path='/' render={() => (<Redirect to='/de/dienstleistungen/geistiges-eigentum' />)} />
    <Route path='/:langURL?/services' component={Services} />
    <Route path='/:langURL?/dienstleistungen' component={Services} />
    <Route path='/services' component={Services} />
    <Route path='/:langURL?/how-it-works' component={HowItWorks} />
    <Route path='/:langURL?/anleitung' component={HowItWorks} />
    <Route path='/:langURL?/features' component={Features} />
    <Route path='/:langURL?/funktionen' component={Features} />
    <Route path='/:langURL?/beliebte-projekte' component={BundleDetails} />
    <Route path='/:langURL?/popular-projects' component={BundleDetails} />
    <Route component={Error} />
</Switch>