react-router v6 路由全局管理和路由拦截

路由全局管理

在src/router/ routes.tsx文件内填写路由配置:

import Oaindex from '@/pages/Oaindex'
import Login from '@/pages/Login'

// 定义路由中的元素类型
interface Router {
    path: string;
    name?: string;
    children?: Array<Router>;
    element: any;
}

// 全局路由配置
const routes: Array<Router> = [
    {
        path: '/oaindex',
        name: 'oaindex',
        element: <Oaindex />
    },
    {
        path: '/login',
        name: 'login',
        element: <Login />,
    },
]

export default routes;

在项目入口文件src/index.tsx里引入router组件:

  • 路由 browser / history 模式使用BrowserRouter,hash 模式使用HashRouter
import React from 'react';
import ReactDOM from 'react-dom/client';
import './css/index.css';
import App from './App';
import { BrowserRouter } from 'react-router-dom'

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

项目入口组件src/App.tsx里引入routes配置:

import React from 'react';
import './css/App.css';
import { useRoutes } from 'react-router-dom'
import routes from '@/router/routes'


function App() {
  const elements = useRoutes(routes)
  return <div className="App">{elements}</div>;
}

export default App;

 全局路由拦截

在src/router/ routes.tsx文件内添加路由拦截,并重新转化路由:

import { Navigate, useRoutes } from 'react-router-dom'
import routes from './routes'

// 路由拦截
const RouterBeforeEach = (props: { route: any, children: any }) => {
    if (props?.route?.meta?.title) {
        document.title = props.route.meta.title;
    }

    // 判断是否已登录
    const isLogin = localStorage.getItem('Authorization');

    // 未登录 需要身份验证的跳转到登录页面
    if (!isLogin && props?.route?.meta?.requiresAuth) {
        return <Navigate to={'/login'} replace />
    }
    // 已登录 登录页面需要跳转到默认页面
    if (isLogin && ['/login'].includes(props?.route?.path)) {
        return <Navigate to={'/'} replace />
    }

    return props.children
}

// 转化路由
const resolveRoute = (routes: any) => {
    return routes.map((item: any) => {
        const route = { path: item.path, element: item.element, children: item.children };
        // 递归处理子路由
        if (item.children) {
            route.children = resolveRoute(item.children)
        }

        if (item.redirect) {
            // 重定向
            route.element = <Navigate to={item.redirect} />
        } else if (route.element) {
            route.element = <RouterBeforeEach route={item}>{item.element}</RouterBeforeEach>
        } else if (item.component) {
            // element 要接收react.element类型 item.component 是对象 所以要转一下
            route.element = <RouterBeforeEach route={item}>{item.component} </RouterBeforeEach>
        }

        return route
    })
}


export default function Router() {
    return useRoutes(resolveRoute(routes))
}

 更改项目入口组件src/App.tsx中路由引入

import React from 'react';
import './css/App.css';
import Router from '@/router'


function App() {
  return <div className="App"><Router /></div>;
}

export default App;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值