react路由二级嵌套

创建people 父页面分别在 Nav 和 App中引用 

import React from 'react'
import Nav from '../Nav'
import {Link} from 'react-router-dom'
export default class People extends React.Component{
    render(){
        return(
            <div>
                <Nav/>
                People
                <ul>
                    <li>
                        <Link to='/people/student'>学生</Link> 
                    </li>
                    <li>
                        <Link to='/people/worker'>工人</Link>
                    </li>
                </ul>
                {this.props.children}
            </div>
        )
    }

}

创建student子页面

创建worker 子页面

实现子路由的效果 需要修改APP.js的路由

实现效果 

总结

1.创建子页面:People -> student  worker

 2.修改路由配置

  <People>

                <Switch>

                <Route path="/people/student" component={ Student }></Route>

                <Route path="/people/worker" component={ Worker }></Route>

                </Switch>

            </People>

3.在对应要显示的页面people增加显示方案

        { this.props.children }

    4.增加子路由的导航

        <ul>

            <li>

                <Link to="/people/student">学生</Link>

            </li>

            <li>

                <Link to="/people/worker">工人</Link>

            </li>

        </ul>

React 中,使用 React Router 可以轻松地创建二级路由。下面是一个简单的示例来说明如何创建二级路由: 首先,确保你已经安装了 React Router 库。可以使用以下命令来安装: ``` npm install react-router-dom ``` 接下来,在你的应用程序中导入所需的模块: ```jsx import React from 'react'; import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom'; ``` 然后,在你的组件中定义路由。下面是一个示例: ```jsx function App() { return ( <Router> <div> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> <Link to="/dashboard">Dashboard</Link> </li> </ul> </nav> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> <Route path="/dashboard" component={Dashboard} /> </Switch> </div> </Router> ); } function Home() { return <h2>Home</h2>; } function About() { return <h2>About</h2>; } function Dashboard() { return ( <div> <h2>Dashboard</h2> <Switch> <Route path="/dashboard/profile" component={Profile} /> <Route path="/dashboard/settings" component={Settings} /> </Switch> </div> ); } function Profile() { return <h3>Profile</h3>; } function Settings() { return <h3>Settings</h3>; } ``` 在上面的示例中,我们创建了一个 `Router` 组件,并在其中定义了导航菜单和路由规则。`Route` 组件用来定义路径和组件的映射关系。`Switch` 组件用来保证只有一个路由匹配成功。 在 `Dashboard` 组件中,我们创建了另一个嵌套的 `Switch` 组件,以支持二级路由。在这个嵌套的 `Switch` 中,我们定义了 `/dashboard/profile` 和 `/dashboard/settings` 这两个路径与对应的组件的映射关系。 最后,将 `App` 组件渲染到 DOM 中: ```jsx ReactDOM.render(<App />, document.getElementById('root')); ``` 通过以上代码,你就可以在你的应用程序中创建二级路由了。在导航菜单中点击链接将会渲染相应的组件。例如,点击 `/dashboard` 将会显示 `Dashboard` 组件,点击 `/dashboard/profile` 将会显示 `Profile` 组件。 希望这个示例能帮助你理解如何在 React 中创建二级路由。如有其他问题,请随时提问!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值