没有路径的路由将始终匹配,无论您是否为其指定确切的属性,因此
{
path : "",
component : AsOver,
exact: true
},
始终匹配,即使路线是
/customer/cards
要避免这种情况,您可以使用Switch并在
/客户/卡
.
Switch
将渲染第一个匹配的路由,因此使用
path=""
如果
customer/cards
渲染
所以你的路线看起来像
const routes: any = [
{
path : "/",
component : Home,
exact : true
},
{
path : "/about",
component : About,
exact : true
},
{
path : "/customer",
component : Customer,
routes : [
{
path : "/customer/cards",
component : Cards,
exact: true
},
{
path : "",
component : Overview,
exact: true
},
]
}
];
您的客户组件将如下所示
const Customer = ({ routes, location })=>(
<div>
<h2>Customer Index</h2>
<Switch>{routes.map((route, i) => <RouteGeneric key={i} {...route} />)}</Switch>
</div>
)
Working codepen