简体   繁体   English

如何在create-react-app上使用'/'阻止路由

[英]How to block routes with '/' on create-react-app

I'm creating a simply SPA with four pages and I found one big issue that crashes my app. 我正在创建一个包含四个页面的简单SPA,但发现一个使我的应用崩溃的大问题。 I have four routes: 我有四个路线:

<Switch>
     <Route exact path='/przewodnictwo' component={Conductivity} />
     <Route exact path='/apartamenty' component={Apartments} />
     <Route exact path='/przewoz_osob' component={Transport} />
     <Route exact path='/narciarstwo' component={Skiing} />
     <Redirect from='*' to='/przewodnictwo' />
</Switch>

Everything works perfekt and my NavLinks looks like this: 一切正常,我的NavLinks如下所示:

<NavLink exact to='/apartamenty'>
       Apartamenty
</NavLink>

All my images are loaded from public folder. 我的所有图像都是从公用文件夹加载的。 Any other routes like /example , /przewodnictwo/23 are redirecting me to main page which is /przewodnictwo . /example/przewodnictwo/23类的任何其他路由都将我重定向到/przewodnictwo But strange things are happening when I write in url /przewodnictwo/ or /apartamenty/ these are not blocked and my images are being loaded from not public folder but from /public/apartamenty/3.jpg which dosen't exists. 但是,当我在url /przewodnictwo//apartamenty/写入文件时,发生了奇怪的事情,这些没有被阻止,并且我的图像不是从公共文件夹而是从/public/apartamenty/3.jpg加载的,而该文件夹并不存在。

EDIT 编辑

I forgot to add '/' to paths images and now works perfect. 我忘了在路径图像上添加“ /”,现在可以完美使用了。 src='1.jpg' has been changed to src='/1.jpg' Thank you for help. src ='1.jpg'已更改为src ='/ 1.jpg'谢谢您的帮助。

But how to block '/przewodnictwo/' route ?? 但是如何阻止“ / przewodnictwo /”路线呢? I really don't like this url, it's ugly 我真的不喜欢这个网址,很丑

You need to use strict keyword to avoid trailing slash. 您需要使用strict关键字,以免出现斜杠。 According to react-router documentation for strict 根据反应路由器文档进行严格

When true , a path that has a trailing slash will only match a location.pathname with a trailing slash. 设置为true ,带有斜杠的路径将只匹配带有斜杠的location.pathname This has no effect when there are additional URL segments in the location.pathname . location.pathname还有其他URL段时,这无效。

<Switch>
     <Route exact strict path='/przewodnictwo' component={Conductivity} />
     <Route exact strict path='/apartamenty' component={Apartments} />
     <Route exact strict path='/przewoz_osob' component={Transport} />
     <Route exact strict path='/narciarstwo' component={Skiing} />
     <Redirect from='*' to='/przewodnictwo' />
</Switch>

To block the trailing slashes from matching your routes, you can use the strict prop on the Route component. 要阻止尾部斜杠与您的路线匹配,可以在Route组件上使用strict道具。

Source: https://reacttraining.com/react-router/web/api/Route/strict-bool 来源: https : //reacttraining.com/react-router/web/api/Route/strict-bool

<Route>

strict: bool 严格:布尔

When true, a path that has a trailing slash will only match a location.pathname with a trailing slash. 设置为true时,带有斜杠的路径将只匹配带有斜杠的location.pathname。 This has no effect when there are additional URL segments in the location.pathname. 当location.pathname中有其他URL段时,这无效。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM