react路由原理解析(React配置子路由的实现)
类别:编程学习 浏览量:1877
时间:2022-01-18 01:02:25 react路由原理解析
React配置子路由的实现1、组件First.js下有子组件:
import Admin from './Admin' import FormCom from './FormCom' import One from './One' import ButtonCom from './ButtonCom' import MenuCom from './MenuCom' import StepsCom from './StepsCom' import TabsCom from './TabsCom' import TableCom from './TableCom' import MessageCom from './MessageCom' import NotificationCom from './NotificationCom' import ProgressCom from './ProgressCom' import SpinCom from './SpinCom' import BadgeCom from './BadgeCom'
First.js完整代码如下:
import React from 'react' import { Layout, Menu } from 'antd'; import { UserOutlined, LaptopOutlined, NotificationOutlined } from '@ant-design/icons'; import { HashRouter, Route, Link } from 'react-router-dom' import Admin from './Admin' import FormCom from './FormCom' import One from './One' import ButtonCom from './ButtonCom' import MenuCom from './MenuCom' import StepsCom from './StepsCom' import TabsCom from './TabsCom' import TableCom from './TableCom' import MessageCom from './MessageCom' import NotificationCom from './NotificationCom' import ProgressCom from './ProgressCom' import SpinCom from './SpinCom' import BadgeCom from './BadgeCom' const { SubMenu } = Menu; const { Header, Content, Sider } = Layout; export default class First extends React.Component { constructor() { super(); } //只展开当前父级菜单 begin rootSubmenuKeys = ['sub1', 'sub2', 'sub3']; state = { openKeys: ['sub1'], }; onOpenChange = openKeys => { const latestOpenKey = openKeys.find(key => this.state.openKeys.indexOf(key) === -1); if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) { this.setState({ openKeys }); } else { this.setState({ openKeys: latestOpenKey ? [latestOpenKey] : [], }); } }; //只展开当前父级菜单 end render() { return (<li> <Layout> <HashRouter> <Header className="header" style={{ position: 'fixed', zIndex: 1, width: '100%', paddingLeft: '20px'}}> <li className="logo" style={{fontSize: '32px', color: '#ffffff', fontWeight: 'bold'}}>React App</li> <Menu theme="dark" mode="horizontal" defaultSelectedKeys={['1']}> {/*<Menu.Item key="1">nav 1</Menu.Item> <Menu.Item key="2">nav 2</Menu.Item> <Menu.Item key="3">nav 3</Menu.Item>*/} </Menu> </Header> <Layout> <Sider width={200} className="site-layout-background" style={{ overflow: 'auto', height: '100vh', position: 'fixed', left: 0, }}> <Menu theme="dark" mode="inline" defaultSelectedKeys={['2']} defaultOpenKeys={['sub1']} style={{ height: '100%', paddingTop: '60px', borderRight: 0 }} onOpenChange={this.onOpenChange} openKeys={this.state.openKeys} > <SubMenu key="sub1" icon={<UserOutlined />} title="subnav 1"> <Menu.Item key="1"><Link to={`${this.props.match.path}/admin`}>admin</Link></Menu.Item> <Menu.Item key="2"><Link to={`${this.props.match.path}/form`}>form</Link></Menu.Item> <Menu.Item key="3"><Link to={`${this.props.match.path}/one`}>One</Link></Menu.Item> <Menu.Item key="4"><Link to={`${this.props.match.path}/menu`}>Menu</Link></Menu.Item> </SubMenu> <SubMenu key="sub2" icon={<LaptopOutlined />} title="subnav 2"> <Menu.Item key="5"><Link to={`${this.props.match.path}/step`}>Step</Link></Menu.Item> <Menu.Item key="6"><Link to={`${this.props.match.path}/tabs`}>Tabs</Link></Menu.Item> <Menu.Item key="7"><Link to={`${this.props.match.path}/table`}>Table</Link></Menu.Item> <Menu.Item key="8"><Link to={`${this.props.match.path}/message`}>Message</Link></Menu.Item> </SubMenu> <SubMenu key="sub3" icon={<NotificationOutlined />} title="subnav 3"> <Menu.Item key="9"><Link to={`${this.props.match.path}/notification`}>Notification</Link></Menu.Item> <Menu.Item key="10"><Link to={`${this.props.match.path}/progress`}>ProgressCom</Link></Menu.Item> <Menu.Item key="11"><Link to={`${this.props.match.path}/spin`}>Spin</Link></Menu.Item> <Menu.Item key="12"><Link to={`${this.props.match.path}/badge`}>Badge</Link></Menu.Item> <Menu.Item key="13"><Link to={`${this.props.match.path}/button`}>Button</Link></Menu.Item> </SubMenu> </Menu> </Sider> <Layout style={{ padding: '84px 20px 20px', marginLeft: 200}}> <Content className="site-layout-background" style={{ padding: 24, margin: 0 }} > <Route path={`${this.props.match.path}/admin`} exact component={Admin}></Route> <Route path={`${this.props.match.path}/form`} component={FormCom}></Route> <Route path={`${this.props.match.path}/one`} component={One}></Route> <Route path={`${this.props.match.path}/menu`} component={MenuCom}></Route> <Route path={`${this.props.match.path}/step`} component={StepsCom}></Route> <Route path={`${this.props.match.path}/tabs`} component={TabsCom}></Route> <Route path={`${this.props.match.path}/table`} component={TableCom}></Route> <Route path={`${this.props.match.path}/message`} component={MessageCom}></Route> <Route path={`${this.props.match.path}/notification`} component={NotificationCom}></Route> <Route path={`${this.props.match.path}/progress`} component={ProgressCom}></Route> <Route path={`${this.props.match.path}/spin`} component={SpinCom}></Route> <Route path={`${this.props.match.path}/badge`} component={BadgeCom}></Route> <Route path={`${this.props.match.path}/button`} component={ButtonCom}></Route> </Content> </Layout> </Layout> </HashRouter> </Layout> </li>) } }
其中
${this.props.match.path}
是关键。
2、假设还有一个登录组件Login.js,代码如下:
import React from 'react' import { Button } from 'antd'; export default class Login extends React.Component { constructor() { super(); } redirectHandle = () => { console.log("aaa"); this.props.history.push("/home"); } render() { return (<Button type="primary" onClick={()=>this.redirectHandle()}>Primary Button</Button>) } }
假设React项目用的是React脚手架搭建,则在src目录下的App.js文件中设置路由:
render() { return (<HashRouter> <Switch> <Route exact={true} path="/login" component={Login} /> <Route path="/home" component={First} /> <Redirect to='/login' /> {/*/login和/home之外的路由直接跳转到/login*/} </Switch> </HashRouter>) }
App.js完整代码如下:
import React from 'react'; import { notification } from 'antd' import { HashRouter, Route, Switch, Redirect } from 'react-router-dom'; import First from './First'; import Login from './Login'; import './App.css'; class App extends React.Component { constructor() { super(); this.openNotificationWithIcon = type => { notification[type]({ message: 'Notification Title', description: 'This is the content of the notification. This is the content of the notification. This is the content of the notification.', }); } } clickHandle() { console.log("clicked!!!"); } render() { return (<HashRouter> <Switch> <Route exact={true} path="/login" component={Login} /> {/**exact 防止混路由混合 如:输入127.xx.xx.xx/home 中有/login页面*/} <Route path="/home" component={First} /> <Redirect to='/login' /> </Switch> </HashRouter>) } } export default App;
项目目录结构如下:
项目预览效果如下:
到此这篇关于React配置子路由的实现的文章就介绍到这了,更多相关React配置子路由内容请搜索开心学习网以前的文章或继续浏览下面的相关文章希望大家以后多多支持开心学习网!
您可能感兴趣
- reactredux任务处理进度(一文搞懂redux在react中的初步用法)
- vscode react插件(基于visual studio code + react 开发环境搭建过程)
- react常见问题(React编程中需要注意的两个错误)
- vue中的ref(Vue3.0中Ref与Reactive的区别示例详析)
- react和antd管理系统(手把手教你从零开始react+antd搭建项目)
- react路由原理解析(React配置子路由的实现)
- react组件分析(react-diagram 序列化Json解读案例分析)
- vue3 props用法(vue3组合API中setup、 ref、reactive的使用大全)
- react 使用实例(React+高德地图实时获取经纬度,定位地址)
- react hooks详解(React Hooks使用常见的坑)
- react循环有几种方法(React forwardRef的使用方法及注意点)
- react代码质量检查(react如何实现一个密码强度检测器详解)
- react代码展示(教你如何从 html 实现一个 react)
- react动态添加组件属性(react使用antd的上传组件实现文件表单一起提交功能完整代码)
- react执行流程(React开启代理的2种实用方式)
- react路由组件怎么用(无废话快速上手React路由开发)
- 这里输入关键词(怎么输入关键词搜索)
- 得这个 难治病 的人太多了,300个人赶到杭州商量怎么办(得这个难治病的人太多了)
- 经度,世界时间腕表的灵魂(世界时间腕表的灵魂)
- 阿里最新财报公布 三季度营收增长3 ,将增加150亿美元回购额度 在美股价小涨(阿里最新财报公布)
- 赵薇时胖时瘦 最近变美少女 原因在这里 躺着就变瘦(赵薇时胖时瘦最近变美)
- 学会这26种姿势,你就可以和兵哥哥切磋了(你就可以和兵哥哥切磋了)
热门推荐
- python怎么装opencv(Python基于opencv实现的简单画板功能示例)
- css3多边形教学(使用css3绘制出各种几何图形)
- 五分钟学会docker镜像与容器操作(Docker如何制作自己镜像并上传dockerhub)
- .NET获取枚举值的描述
- linux操作系统的关机指令有哪些(Linux系统关机命令之间的区别及用法详解)
- dedecms授权协议(360提示DedeCms全局变量覆盖漏洞临时解决方法)
- js轮播图片(JS实现简单图片轮播效果)
- smart组件详解(Smarty模板类内部原理实例分析)
- 一般用云主机干嘛(免费云主机怎么申请?有什么限制?)
- python切片的用法(Python进阶之全面解读高级特性之切片)