搜索
首页web前端前端问答react路由怎么安装

react路由怎么安装

Jan 03, 2023 pm 02:12 PM
react路由

react路由的安装方法:1、通过“npm i react-router-dom@5.0 -S”安装路由;2、使用“import { HashRouter as Router, Route, NavLink } from 'react-router-dom'”方式导入路由即可。

react路由怎么安装

本教程操作环境: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=&#39;/&#39; exact>首页</NavLink>|
            <NavLink to=&#39;/about&#39; >关于</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 &#39;react-router-dom&#39;
// NavLink 会比 link自动添加一个active的class
function App() {
    return (<Router>
        <div>
            <NavLink to=&#39;/&#39; exact>首页</NavLink>|
            <NavLink to=&#39;/about&#39; >关于</NavLink>
            <NavLink to=&#39;/produce/abc&#39;>产品abc</NavLink>
            <NavLink to=&#39;/produce/123&#39;>产品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(&#39;/&#39;)}>回到首页</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 &#39;react-router-dom&#39;
// NavLink 会比 link自动添加一个active的class
function App() {
    return (<Router>
        <div>
            <NavLink to=&#39;/&#39; exact>首页</NavLink>|
            <NavLink to=&#39;/about&#39; >关于</NavLink>
            <NavLink to=&#39;/produce/abc&#39;>产品abc</NavLink>
            <NavLink to=&#39;/produce/123&#39;>产品123</NavLink>
            <NavLink to=&#39;/admin&#39;>管理</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=&#39;/admin&#39; 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(&#39;/&#39;)}>回到首页</button>
    </div>)
}
 
function Home() {
    return <div>首页页面</div>
}
 
function About() {
    return <div>关于页面
    </div>
}

推荐学习:《react视频教程

以上是react路由怎么安装的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
CSS:使用ID选择器不好吗?CSS:使用ID选择器不好吗?May 13, 2025 am 12:14 AM

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

HTML5:2024年的目标HTML5:2024年的目标May 13, 2025 am 12:13 AM

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

HTML5试图改进的主要领域是什么?HTML5试图改进的主要领域是什么?May 13, 2025 am 12:12 AM

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

CSS ID和类:常见错误CSS ID和类:常见错误May 13, 2025 am 12:11 AM

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

课程和ID选择器之间的差异是什么?课程和ID选择器之间的差异是什么?May 12, 2025 am 12:13 AM

classSelectorSareVersAtileAndReusable,whileIdSelectorSareEctorSareEniqueAndspecific.1)useclassSelectors(表示)

CSS IDS vs类:真正的差异CSS IDS vs类:真正的差异May 12, 2025 am 12:10 AM

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

CSS:如果我只使用课程怎么办?CSS:如果我只使用课程怎么办?May 12, 2025 am 12:09 AM

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

CSS中的ID和类选择器:初学者指南CSS中的ID和类选择器:初学者指南May 12, 2025 am 12:06 AM

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

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

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

热门文章

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。