路由匹配与老师讲的不一致
来源:4-3 使用动态路由获取不同列表内容(3)
fangfangfang2019
2019-07-22 09:52:12
见截图,地址输入/detail,/和/detail对应的路由都能匹配上,但是输入/只会匹配到/对应的路由。
加不加exact={true}结果都一样。
代码如下:
ReactDOM.render(
<BrowserRouter>
<Layout className="layout">
<Header className="header">
<AppHeader></AppHeader>
</Header>
<Content className="content">
<Route path="/detail" exact={true} component={Detail}></Route>
<Route path="/:id?" exact={true} component={PageList}></Route>
</Content>
<Footer className="footer">@copyright mxf 2019</Footer>
</Layout>
</BrowserRouter >
, document.getElementById('root'));
2回答
_是你_
2019-07-23
是精确匹配,可是不是说添加无效么。不知道是不是顺序影响了,让你试一下,不能特别确定不生效的原因。排查下。不加exact,不加switch,是/能匹配到/和/detail,加了之后,能匹配到各自对应的。
_是你_
2019-07-22
肯定都能匹配到呀,/的话,匹配的应该是下面那个吧,加了问号之后就可以匹配。
至于值匹配“/”这个或许是跟循环有关系的。就是你输入/detail的时候,没有匹配"/"的内容的原因是因为这个在上面先匹配了这个,你调换顺序,在使用exact试下。
相似问题
回答 1
回答 2