react路由的安装方法:1、通过“npm i react-router-dom@5.0 -S”安装路由;2、使用“import { HashRouter as Router, Route, NavLink } from 'react-router-dom'”方式导入路由即可。
本教程操作环境:Windows10系统、react-router-dom5.0版、Dell G3电脑。
react路由怎么安装?
react 路由安装及简单使用
一.react安装路由
安装:npm i react-router-dom@5.0 -S (最新版本为6.0)
导入:
import { HashRouter as Router, Route, NavLink } from 'react-router-dom'
路由配置:
1.所有路由相关内容都应该放在a7f2cf15f06fbef780c6b2609731da81组件内
2.链接使用:bf6dba05d0419787827c9d251c01f231
3.页面使用:1156da80f7a4e718db60a91c54115adf
示例:简单的路由跳转
function App() { return (<Router> <div> //exact 精确匹配 <NavLink to='/' exact>首页</NavLink>| <NavLink to='/about' >关于</NavLink> </div> {/* 路由页面 */} <Switch> <Route path="/" exact component={Home}></Route> <Route path="/about" component={About}></Route> </Switch> </Router>) } export default App function Home() { return <div>首页页面</div> } function About() { return <div> 关于页面 </div> }
二.路由传参
路由传参形式
1.链接传参 5821aa73f26a1d9c1c2c073f2ec66e4a
2.参数传参 f0d4d565475763b37f3f77acc2f0ec9d
3.参数获取 props.match.params.id
常用历史操作的方法
1.go()历史跳转记录
2.goBack(返回)
3.push()添加记录跳转
4.replace()替换记录并跳转
示例:
import { HashRouter as Router, Route, NavLink, Redirect, Switch, Prompt, Link } from 'react-router-dom' // NavLink 会比 link自动添加一个active的class function App() { return (<Router> <div> <NavLink to='/' exact>首页</NavLink>| <NavLink to='/about' >关于</NavLink> <NavLink to='/produce/abc'>产品abc</NavLink> <NavLink to='/produce/123'>产品123</NavLink> </div> {/* 路由页面 */} <Switch> <Route path="/" exact component={Home}></Route> <Route path="/about" component={About}></Route> <Route path="/produce/:id" component={Produce}></Route> </Switch> </Router>) } export default App function Produce({ match, history, location }) { // console.log(match); return (<div> <h1>产品{match.params.id}</h1> <button onClick={() => history.goBack()}>返回</button> <button onClick={() => history.push('/')}>回到首页</button> </div>) } function Home() { return <div>首页页面</div> } function About() { return <div>关于页面 </div> }
子路由传参
示例:
// 导入路由相关组件 // 导入哈希路由 别名router // Route路由页面 // NvaLink 导航链接 import { HashRouter as Router, Route, NavLink, Redirect, Switch, Prompt, Link } from 'react-router-dom' // NavLink 会比 link自动添加一个active的class function App() { return (<Router> <div> <NavLink to='/' exact>首页</NavLink>| <NavLink to='/about' >关于</NavLink> <NavLink to='/produce/abc'>产品abc</NavLink> <NavLink to='/produce/123'>产品123</NavLink> <NavLink to='/admin'>管理</NavLink> </div> {/* 路由页面 */} <Switch> <Route path="/" exact component={Home}></Route> <Route path="/about" component={About}></Route> <Route path="/produce/:id" component={Produce}></Route> <Route path="/admin" component={Admin}></Route> </Switch> </Router>) } export default App function NoMatch({ location, history }) { return (<div> <h1>404</h1> <p>你爹来咯</p> <p>{location.url}</p> <button onClick={history.goBack}>后退</button> <NavLink to={{ pathname: "/" }}>首页</NavLink> </div>) } function Admin() { return (<div style={{ "display": "flex" }}> <div style={{ width: "200px", boderRight: "1px solid #f0f0f0" }}> <NavLink to="/admin/dash">概览</NavLink><br /> <NavLink to="/admin/orderlist">列表</NavLink> </div> <div> <Route path="/admin/dash" component={Dash}></Route> <Route path="/admin/orderlist" component={OrderList}></Route> <Redirect path='/admin' to="/admin/dash"></Redirect> </div> </div>) } function Dash() { return (<div> 概览 </div>) } function OrderList() { return (<div> 订单列表 </div>) } function Produce({ match, history, location }) { // console.log(match); return (<div> <h1>产品{match.params.id}</h1> <button onClick={() => history.goBack()}>返回</button> <button onClick={() => history.push('/')}>回到首页</button> </div>) } function Home() { return <div>首页页面</div> } function About() { return <div>关于页面 </div> }
推荐学习:《react视频教程》
以上是react路由怎么安装的详细内容。更多信息请关注PHP中文网其他相关文章!

使用ID选择器在CSS中并非固有地不好,但应谨慎使用。1)ID选择器适用于唯一元素或JavaScript钩子。2)对于一般样式,应使用类选择器,因为它们更灵活和可维护。通过平衡ID和类的使用,可以实现更robust和efficient的CSS架构。

html5'sgoalsin2024focusonrefinement和optimization,notnewfeatures.1)增强performandemandeffifice throughOptimizedRendering.2)risteccessibilitywithrefinedibilitywithRefineDatientAttributesAndEllements.3)expliencernsandelements.3)explastsecurityConcerns,尤其是withercervion.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供应,2)语义结构,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,简化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

classSelectorSareVersAtileAndReusable,whileIdSelectorSareEctorSareEniqueAndspecific.1)useclassSelectors(表示)

IDSareuniqueIdentifiersForsingLelements,而LileclassesstyLemultiplelements.1)useidsforuniquelementsand andjavascripthooks.2)useclassesforporporporblesable,flexiblestylestylestylinglingactossmultiplelements。

使用仅类选择器可以提高代码的重用性和可维护性,但需要管理类名和优先级。1.提高重用性和灵活性,2.组合多个类创建复杂样式,3.可能导致冗长类名和优先级问题,4.性能影响微小,5.遵循最佳实践如简洁命名和使用约定。

ID和class选择器在CSS中分别用于唯一和多元素的样式设置。1.ID选择器(#)适用于单一元素,如特定导航菜单。2.Class选择器(.)用于多元素,如统一按钮样式。应谨慎使用ID,避免过度特异性,并优先使用class以提高样式复用性和灵活性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6
视觉化网页开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。