• Ant Design+react 路由跳转


    今天我们来继续探讨react的路由跳转

    首先,创建router文件夹中的index

    1. import { lazy } from "react";
    2. import { Outlet,useRoutes } from 'react-router-dom';
    3. //引入页面,引用了路由懒加载
    4. const One = lazy(() => import('../pages/one'));
    5. const Two = lazy(() => import('../pages/two'));
    6. const Three = lazy(() => import('../pages/three'));
    7. //设置页面的路由路径
    8. const routes = [
    9. {
    10. path: '/one',
    11. element: <One/>,
    12. },
    13. {
    14. path: '/two',
    15. element: <Two/>,
    16. children: []
    17. },
    18. {
    19. path: '/three',
    20. element: <Three/>,
    21. }
    22. ];
    23. const WrappedRoutes = () => {
    24. return useRoutes(routes);
    25. };
    26. export default WrappedRoutes;

    之后就是App.js页面了,我的layout是写在app.js里的,所以要在app.js进行设置

    1. import React, { useState,lazy } from 'react';
    2. import {
    3. MenuFoldOutlined,
    4. MenuUnfoldOutlined,
    5. UploadOutlined,
    6. UserOutlined,
    7. VideoCameraOutlined,
    8. } from '@ant-design/icons';
    9. import { Layout, Menu, Button, theme } from 'antd';
    10. import WrappedRoutes from './router/index'; // 引入路由表
    11. import { useLocation, useNavigate,Routes, Route } from 'react-router-dom';
    12. const One = lazy(() => import('./pages/one'));
    13. const Two = lazy(() => import('./pages/two'));
    14. const Three = lazy(() => import('./pages/three'));
    15. const { Header, Sider, Content } = Layout;
    16. const App: React.FC = () => {
    17. const [collapsed, setCollapsed] = useState(false);
    18. const {
    19. token: { colorBgContainer },
    20. } = theme.useToken();
    21. const navigate = useNavigate();
    22. const { pathname } = useLocation(); // 获取当前url
    23. const handleClick = (e: any) => {
    24. // 获取当前点击事件的key值,key值就是我们给页面配置的路由啦
    25. console.log('key', e.key);
    26. navigate(e.key,{replace:true}); // 实现跳转
    27. }
    28. return (
    29. <Layout>
    30. <Sider trigger={null} collapsible collapsed={collapsed}>
    31. <div className="demo-logo-vertical" />
    32. <Menu
    33. theme="dark"
    34. mode="inline"
    35. selectedKeys={[pathname]}
    36. onClick={handleClick} //给侧边栏item设置点击事件
    37. defaultSelectedKeys={['1']}
    38. items={[
    39. {
    40. key: '/one',
    41. icon: <UserOutlined />,
    42. label: 'nav 1',
    43. },
    44. {
    45. key: '/two',
    46. icon: <VideoCameraOutlined />,
    47. label: 'nav 2',
    48. },
    49. {
    50. key: '/three',
    51. icon: <UploadOutlined />,
    52. label: 'nav 3',
    53. },
    54. ]}
    55. />
    56. </Sider>
    57. <Layout>
    58. <Header style={{ padding: 0, background: colorBgContainer }}>
    59. <Button
    60. type="text"
    61. icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
    62. onClick={() => setCollapsed(!collapsed)}
    63. style={{
    64. fontSize: '16px',
    65. width: 64,
    66. height: 64,
    67. }}
    68. />
    69. </Header>
    70. <Content
    71. style={{
    72. margin: '24px 16px',
    73. padding: 24,
    74. minHeight: 280,
    75. background: colorBgContainer,
    76. }}
    77. >
    78. {/*路由出口 */}
    79. <Routes>
    80. <Route exact path="/one" element={<One />} />
    81. <Route exact path="/two" element={<Two />} />
    82. <Route exact path="/three" element={<Three />} />
    83. </Routes>
    84. </Content>
    85. </Layout>
    86. </Layout>
    87. );
    88. };
    89. export default App;

    到这里还有最重要的一步,因为我们这是的路由懒加载,这个时候跳转是会报错的

    一直出现不能读到pathname,从的时候就出现这个问题,那么问题估计出现在这个标签上。找不到路径名,那就是没有找到地址于是我把换成了,就可以了。

    1. import React,{ Suspense } from 'react';
    2. import ReactDOM from 'react-dom/client';
    3. import './index.css';
    4. import App from './App';
    5. import reportWebVitals from './reportWebVitals';
    6. import { BrowserRouter } from 'react-router-dom';
    7. const root = ReactDOM.createRoot(document.getElementById('root'));
    8. root.render(
    9. <React.StrictMode>
    10. <BrowserRouter>
    11. {/* 在使用组件懒加载的时候,在外面套一个react的组件:Suspense ,否则会报错没有 */}
    12. <Suspense>
    13. <App />
    14. </Suspense>
    15. </BrowserRouter>
    16. </React.StrictMode>
    17. );
    18. reportWebVitals();

    最后,在使用路由懒加载的同时,一定要记得使用嵌套,否则还是会报错,切记切记

  • 相关阅读:
    《2022 社交泛娱乐出海白皮书》发布,最全出海破局指南
    MATLAB 2022b 中设置关闭 MATLAB 之前进行询问
    【力扣题解】2155. 分组得分最高的所有下标
    CP Autosar——EthIf配置
    Unity 向量计算、欧拉角与四元数转换、输出文本、告警、错误、修改时间、定时器、路径、
    LeetCode - 141. 环形链表 (C语言,快慢指针,配图)
    sparksession对象简介
    使用python-opencv检测图片中的人像
    golang版本管理gvm
    Linux 文件访问权限说明
  • 原文地址:https://blog.csdn.net/weixin_56848461/article/details/133744875