reactjs - 404页面 - 语言参数缺少处理问题 [英] reactjs - 404 page - and language param missing handling issues

查看:135
本文介绍了reactjs - 404页面 - 语言参数缺少处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to reactjs - working on a site with a language switch I am trying to create an error component to use as a 404 page - but also trying to see how to configure/automatically switch the site to use a default language if the parameter is not defined or to set the error path to still listen out for the lang param.

router.js目前看起来像这样

router.js currently looks like this

    <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>


推荐答案

我想你说的问题是你希望用户能够在URL中不提供语言参数?现在,如果他们不提供它们,他们会被抛入Error组件?您可以使用:langURL?

I think you're saying the problem is that you want the user to be able to not provide the language parameter in the URL? And that right now if they don't provide it they're thrown into the Error component? You can make that parameter optional by using :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>

这篇关于reactjs - 404页面 - 语言参数缺少处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆