React.js实战之Router原理及 React-router

本文介绍 React Router 的核心概念,包括 Hash 路由与 H5 历史 API 路由的区别及应用实例。通过具体代码展示了如何使用 React Router 在 Web 应用中实现导航与路由管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网文档
https://reacttraining.com/react-router/core/guides/philosophy

img_785f519cb0211f99d072c62f471abe7c.png

img_ff7c397222695deb1605ea1f765996bf.png

页面路由

img_22dd6e091b8cb62a839f286f2e536ed2.png

Hash 路由

img_b561ff5c699eaefda9c870f3701ab100.png

img_4c17a25c7340780e46280808d73da5b1.png

H5路由

只对后退记录有效


img_522b17be923687c42c3c4352627bef1b.png

img_a461778a9b678d81da638d42f21c6558.png
// 页面路由
window.location.href = 'http://www.baidu.com';
history.back();

// hash 路由
window.location = '#hash';
window.onhashchange = function(){
    console.log('current hash:', window.location.hash);
}

// h5 路由
// 推进一个状态
history.pushState('name', 'title', '/path');
// 替换一个状态
history.replaceState('name', 'title', '/path');
// popstate
window.onpopstate = function(){
    console.log(window.location.href);
    console.log(window.location.pathname);
    console.log(window.location.hash);
    console.log(window.location.search);
}
img_373f138072e5a28822383097a5f483ac.png
// react-router
import React from 'react';
import ReactDOM from 'react-dom';
import { HashRouter as Router, Switch, Route, Link } from 'react-router-dom'


class A extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return (
            <div>
                Component A

                <Switch>
                    <Route exact path={`${this.props.match.path}`} render={(route) => {
                        return <div>当前组件是不带参数的A</div>
                    }}/>
                    <Route path={`${this.props.match.path}/sub`} render={(route) => {
                        return <div>当前组件是Sub</div>
                    }}/>
                    <Route path={`${this.props.match.path}/:id`} render={(route) => {
                        return <div>当前组件是带参数的A, 参数是:{route.match.params.id}</div>
                    }}/>
                </Switch>
            </div>
        )
    }
}

class B extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return <div>Component B</div>
    }
}

class Wrapper extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return (
            <div>
                <Link to="/a">组件A</Link>
                <br/>
                <Link to="/a/123">带参数的组件A</Link>
                <br/>
                <Link to="/b">组件B</Link>
                <br/>
                <Link to="/a/sub">/a/sub</Link>
                {this.props.children}
            </div>
        );
    }
}

ReactDOM.render(
    <Router>
        <Wrapper>
            <Route path="/a" component={A}/>
            <Route path="/b" component={B}/>
        </Wrapper>
    </Router>,
    document.getElementById('app')
);

通过以上代码,首先演示 Hash 路由


img_3b005ed0bf852867da2fc04295d472d7.png

再演示 H5路由,即修改此处


img_174c6101667b55f8cf6e733bc396887b.png

img_726c67dda1963c43b8603605c9a87032.png

将参数传给组件
img_e66d8447ce9134d21000427ea3c12e5d.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值