在 React Router v5 中,当你在父级路由上使用 exact 属性时,它意味着该路由只有在其 path 完全匹配当前 URL 时才会被激活。这意味着,如果父级路由精确匹配了,React Router 就不会继续向下查找子路由,因为 exact 限制了匹配范围。
例如,假设你有以下路由配置:
<Route exact path="/parent" component={ParentComponent}>
<Route path="/parent/child" component={ChildComponent} />
</Route>
在这个例子中,如果你访问 /parent,因为父级路由设置了 exact,它将匹配并仅渲染 ParentComponent。即使存在一个子路由 /parent/child,由于父级路由已经精确匹配,React Router 不会继续尝试匹配子路由,因此 ChildComponent 不会被渲染。
如果你希望同时能够访问父级和子级组件,应该移除父级路由上的 exact 属性,让路由系统有机会继续匹配子路由:
<Route path="/parent">
<Route path="/parent/child" component={ChildComponent} />
</Route>
这样配置后,访问 /parent 会渲染 ParentComponent,而访问 /parent/child 则会同时渲染 ParentComponent(作为容器)和 ChildComponent。如果没有其他路由与 /parent 完全匹配,就不需要 exact,因为默认情况下 Route 并不要求完全匹配。