每次用配置react
路由都会考虑是否应该给给<Route>
组件加上exact
或strict
。下面妹子将于自认为比较清晰的方式列举出来什么场景须要加和不加。javascript
本文案例主要以react-router v4+为主,v5版本是由于发布时版本依赖有问题而直接跳成这个大版本的,用法和4彻底相同,就是这么任性 > < ,升级详情可看本文最后连接
exact
默认为false,若是为true时,须要和路由相同时才能匹配,可是若是有斜杠也是能够匹配上的。
若是在父路由中加了exact
,是不能匹配子路由的,建议在子路由中加exact
,以下所示java
//父路由 <Switch> <Route path="/a" component={ComponentA} /> </Switch>
//子路由,tuanDetail组件里 <Switch> <Route path="/a/b" exact component={ComponentB}/> </Switch>
<Route strict path="/one" component={About} />react
strict
默认为false,若是为true时,路由后面有斜杠而url中没有斜杠,是不匹配的git
若是没有子路由的状况,建议你们配都加一个exact
;若是有子路由,建议在子路由中加exact
,父路由不加;
而strict
是针对是否有斜杠的,通常能够忽略不配置。github
原文地址: https://raoenhui.github.io/react/2019/05/04/exact-strict/
https://reacttraining.com/react-router/web/api/Route/exact-bool
https://reacttraining.com/blog/react-router-v5/
Happy coding .. :)web