我试图将ShopPage组件中的CollectionPage组件显示为一个嵌套路由,该路由接收collectionId作为参数,但我得到一个空页面。 这些是我的组件。
商店页面:
import React from 'react';
import CollectionsOverview from '../../components/collections-overview/collections-overview.component';
import { Route } from 'react-router-dom'
import CollectionPage from '../collection/collection.component';
const ShopPage = ({ match }) => (
<div className='shop-page'>
< Route exact path={`${match.path}`} component={CollectionsOverview} />
<Route exact path={`${match.path}/collectionId`} component={CollectionPage} />
</div>
)
export default ShopPage;
CollectionPage组件:
import React from 'react';
import CollectionItem from '../../components/collection-item/collection-item.component';
import './collection.styles.scss';
const CollectionPage=({match})=>{
console.log(match);
return(
<div className="collection-page">
<h2>Collection Page</h2>
</div>
)}
export default CollectionPage;
答案 0 :(得分:2)
似乎collectionId
是一个应以:
为前缀的参数,例如:
<Route exact path={`${match.path}/:collectionId`} component={CollectionPage} />
并从App.tsx中的exact
中删除<Route path="/shop" component={ShopPage} />
,检查this