我的项目中路由无法正常工作的原因是什么?
当我在地址之后键入“/创建”
时,我希望它显示home
组件。由于我不使用精确
prop。因为path ='/c'
与“/create”
匹配,但它显示create
component。它首先工作正常,当我从精确
prop中使用时,它可以按预期工作正常。我不知道为什么从第一个route
中删除Exact
后,它不按预期工作。
仅当我做:< outa path =“/” element = {< home/>}/>
注意:我将其用于运行服务器: npx json-server -watch数据/db.json -port 8000
请参阅codeSandBox上: https://codesandbox.io/s/epic-feather-uz2iur
这是我的路线部分:
function App() {
return (
<Router>
<div className="App">
<Navbar />
<div className="content">
<Routes>
<Route path="/c" element={<Home />} />
<Route path="/create" element={<Create />} />
</Routes>
</div>
</div>
</Router>
);
}
它在我的webstorm上给出了相同的结果(react-router-dom版本5 ):
<Router>
<div className="App">
<Navbar />
<div className="content">
<Switch>
<Route path="/">
<Home />
</Route>
<Route path="/create">
<Create />
</Route>
</Switch>
</div>
</div>
</Router>
When I type "/create"
after the address, I expect it shows Home
component. Since I don't use from exact
prop. Because path='/c'
matches with "/create"
, but it shows Create
component. It first worked fine and when I used from exact
prop it worked fine as expected. I don't know why it does not work as expected after I removed exact
from the first Route
.
It works fine only if I do: <Route path="/" element={<Home />} />
Note: I used this for run server: npx json-server --watch data/db.json --port 8000
See that on Codesandbox: https://codesandbox.io/s/epic-feather-uz2iur
It's the section of my Routes:
function App() {
return (
<Router>
<div className="App">
<Navbar />
<div className="content">
<Routes>
<Route path="/c" element={<Home />} />
<Route path="/create" element={<Create />} />
</Routes>
</div>
</div>
</Router>
);
}
It gives the same result on my Webstorm(react-router-dom version 5):
<Router>
<div className="App">
<Navbar />
<div className="content">
<Switch>
<Route path="/">
<Home />
</Route>
<Route path="/create">
<Create />
</Route>
</Switch>
</div>
</div>
</Router>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎问题是对路由路径匹配如何从
react-router@5
转换为react> react-router@6
的误解。在RRDV5中,路线路径更像是“路径前缀”,这意味着它的行为就像您描述的那样。
path =“/c”
可以匹配 url路径以“/c”/c“”
开始。当尝试具体或完全匹配某些路线时,这具有其特殊性。这就是为什么幼稚的开发人员会用eact
prop prop pepper prop() )。例如,您的V5版本,它需要使用
Exact
prop才能匹配“/c”
并渲染home
component ,或者完全不匹配,因此可以匹配创建
组件。这是因为(a)路由匹配在V5中的工作方式,以及(b)
switch
组件渲染 唯一的方式路由
或重定向
组件。路线路径顺序和特异性很重要!在几乎100%的用例中,您不需要精确的
支架,只需正确订购路线,从 最具体的 到 最不具体的 。相同的V5路由和切换,但正确订购:
请注意,
“/c”
无法匹配path =“/create”
,并且匹配一直延续到找到它和匹配路径=“/c”
。现在,在rrdv6中, 始终 完全匹配,因此
exack ecker
prop不再存在,因为它不需要。每条路由都使用路径排名系统,请参见等级路线。路径的特异性产生等级值,这用于匹配路由。路线订单现在无关紧要。It seems the issue is a misunderstanding of how route path matching changed from
react-router@5
toreact-router@6
.In RRDv5 the route path was more of a "path prefix", meaning it behaved like you described. A
path="/c"
could match any URL path starting with"/c"
. This had its peculiarities when trying to specifically, or exactly, match certain routes. This is often why naive devs would pepper all their routes with theexact
prop (because not understanding how matching worked).For example, your v5 version, it would need to use the
exact
prop in order to match"/c"
exactly and render theHome
component, or not match exactly so theCreate
component could be matched.This is because of (A) the way route matching works in v5, and (B) the way the
Switch
component renders only the first matchingRoute
orRedirect
component. The route path order and specificity matters! In almost 100% of the use cases you don't need theexact
prop by simply ordering your routes correctly, from most specific to least specific.The same v5 routes and switch, but ordered correctly:
Note now that
"/c"
can't match topath="/create"
and matching continues down until it finds and matchespath="/c"
.In RRDv6 paths are now always exactly matched, so the
exact
prop no longer exists as it's not needed. Each route uses a path ranking system, see Ranking Routes. The specificity of the path yields a rank value and this is used to match a route. Route order is now irrelevant.使用
React-Router-dom
版本5您可以将switch
与*
一起使用,并且应该根据文档
工作示例
Using
react-router-dom
version 5 you can useSwitch
along with*
and it should workAccording to documentation
Working example
关于“精确” prop的V6发生了破坏的变化 - 它不再存在,默认情况下所有路径均为精确。
您可以在此处阅读更多信息:升级到v6
There was a breaking change in v6 regarding "exact" prop - it does not exist anymore and all paths are by default exact.
You can read more here: upgrading to v6