我想使用 react-router-dom 在我的反应应用程序中创建一个动态路由。我一直在阅读有关它的文件,但在我的情况下,这些都没有真正意义。我有一个项目页面,然后您可以单击项目页面中的链接,它会将您带到一个名为项目详细信息的新页面。每个网址都不一样。
应用程序.js
<BrowserRouter>
<Switch>
<Route path="/" component={Home} exact />
<Route path="/about" component={About} exact />
<Route path="/projects" component={Projects} exact />
<Route path="/workshops" component={Workshops} exact />
<Route path="/bodywork" component={Bodywork} exact />
<Route path="/contact" component={Contact} exact />
<Route path="/:projectdetails" component={ProjectDetails} exact />
</Switch>
</BrowserRouter>
有十个不同名称的不同项目。它们在这样的数据文件中:
export const ObjOne = {
title: 'Feldbewegung',
img: './images/bodyOfEarth.jpg',
alt: 'Feldbewegung',
link: '/feldbewegung-details'
};
export const ObjTwo = {
title: 'Nonmaterial city beautification',
img: './images/bodyOfEarth.jpg',
alt: 'Nonmaterial city beautification',
link: '/nonmaterial-city-beautification-details'
};
export const ObjThree= {
title: 'Body of Earth',
img: './images/bodyOfEarth.jpg',
alt: 'Body of Earth',
link: '/body-of-earth-details'
};
例如有三个。他们被传递到 Projects.js
import { ObjOne, ObjTwo, ObjThree, ObjFour, ObjFive, ObjSix, ObjSeven, ObjEight, ObjNine, ObjTen} from '../components/Data/projectsData';
function ProjectImage({img, alt, link, title}) {
return (
<>
<div className="ProjectImage">
<img className="project-img" src={img} alt={alt} />
<a className="project-link" href={link}>{title}</a>
</div>
</>
)
}
function Projects({}) {
return (
<>
<Navbar1 />
<div className="page">
<Container className="projects-container">
<ProjectImage {...ObjOne} />
<ProjectImage {...ObjTwo} />
<ProjectImage {...ObjThree} />
...continuing to ObjTen..
有没有办法以某种方式添加动态路由或页面?