React-router中exact

1.exact为true或false的区别   

  exact:一般而言,react路由会匹配所有匹配到的路由组件,exact能够使得路由的匹配更严格一些。exact的值为bool型,为true是表示严格匹配,要求路径与location.pathname必须完全匹配,且会比较匹配过的路由是否和正匹配的路由一致,为false时为正常匹配。

        如在exact为true时,’/link’与’/’是不匹配的,但是在false的情况下它们又是匹配的。

<Route path='/' component={App} />
<Route path='/Home' component={Home} />
<Route path='/About' component={About} />//这种情况下,如果点击Home,匹配路由path='/Home',那么会把App也会展示出来。
 
<Route exact path='/' component={App} />
<Route path='/Home' component={Home} />
<Route path='/About' component={About} />
//这种情况,如果点击Home,匹配路由path='/Home',那么App就不会展示出来

参考地址:react的Router的exact、path、component、strict属性_hehepeng123的博客-CSDN博客

posted @ 2021-06-28 14:19  留下成长的足迹  阅读(744)  评论(0编辑  收藏  举报