React configures page routing like vue, and supports redirect routing, routing guards, etc. (completed using useRoutes)...

  • I hope to achieve the vuesame result as in . In jsthe configuration, basic operations such as route redirection are completed. I am not used to using Routes、Routeetc. to wrap each other.

  • All are based on [email protected]encapsulating a routing component and displaying a routing guard component.

  • Routing component- ExRouter.tsx:<ExRouter routes={routes}></ExRouter>

    Extend routing configuration, support redirection, and facilitate the expansion of other attributes.

    ```js // 基于 [email protected] 封装 import { useRoutes, Navigate, IndexRouteObject, NonIndexRouteObject } from 'react-router-dom' import { useLocation, Location } from 'react-router'

    /**

    • @description: 扩展 IndexRouteObject / interface exIndexRouteObject extends IndexRouteObject { /*
      • @description: redirect routing address*/ redirect?: string }

    /**

    • @description: 扩展 NonIndexRouteObject / interface exNonIndexRouteObject extends NonIndexRouteObject { /*
      • @description: redirect routing address*/ redirect?: string }

    /**

    • @description: Route object type / export type exRouteObject = exIndexRouteObject | exNonIndexRouteObject / *
    • @description: Find the route object type */ export type findExRouteObject = exRouteObject | undefined

    /**

    • @description: component parameters /type props = {/ *
      • @description: route list / routes: exRouteObject[], / *
      • @description: list of child components*/ children?: any }

    const Component = (props: props) => { // Current navigation object const location = useLocation() // Find route object const findRoute = (routes: exRouteObject[], location: Location): findExRouteObject => { // Current level Check one round let route: any = routes.find((item: any) => item.path === location.pathname) // If not, search for subpages of the same level at the current level if (!route) { // Check, Find and stop routes.some((item: any) => { // Take out the sublist const children: exRouteObject[] = item?.children || [] // Check route = findRoute(children, location) // There is a value Then pause return !!route }) } // Return return route } // Find the current route const route: findExRouteObject = findRoute(props.routes, location) // Return to rendering return ( <> {/* Load all routes/} { useRoutes(props.routes) } {/ Check whether the current route requires redirection*/} { route?.redirect && } > ) }

    export default Component ```

  • Route interception (route guard) component:<BeforeEach></BeforeEach>

    ```js // import { Navigate, useLocation, useSearchParams } from 'react-router-dom'

    const Component = (props: any) => { // // 接收路由参数 // const [searchParams] = useSearchParams() // // 当前导航对象 // const location = useLocation() // // token (检查本地或路由参数) // const token = 'xxxx' // // console.log(location, searchParams.get('token')) // // 路由权限校验 // if (location.pathname.includes('/login') && token) { // // 跳转登录页 && token有值 // return // } else if (!location.pathname.includes('/login') && !token) { // // 不是跳转登录页 && token无值 // return // } // 验证通过 return props.children }

    export default Component ```

  • 上面两个组件在路由中的使用:

    ```js import React from 'react' import { Navigate } from 'react-router-dom' import BeforeEach from './BeforeEach' import ExRouter from './ExRouter'

    // 懒加载 const lazyload = (path: string) => { // 加载组件 let Component=React.lazy(()=>{return import (@/${path})}) // 返回渲染 return ( }> ) }

    // 基础路由 const baseRoutes: Record [] = [ { path: '/home', element: lazyload('views/home'), }, { path: '/user', element: lazyload('views/user'), }, { path: '/layout', redirect: '/layout/home', element: lazyload('layouts/BaseLayout'), children: [ { path: '/layout/home', redirect: '/layout/home/home1', element: lazyload('views/home'), children: [ { path: '/layout/home/home1', element: lazyload('views/home') } ] } ] } ]

    // 路由列表 const routes: Record [] = [ ...baseRoutes, { path: "/404", element: (<>页面地址不存在>), }, { path: "/", element: }, { path: "*", element: }, ]

    // 加载配置式路由 function Router () { return ( {/* 加载所有路由 /} {/ { useRoutes(props.routes) } /} {/ 加载所有路由,并扩展路由配置 */} ) }

    // 导出 export default Router ```

    test.gif

Guess you like

Origin blog.csdn.net/zz00008888/article/details/132974424