React 像 vue 一样配置页面路由,并支持重定向路由,路由守卫等(使用 useRoutes 完成)...

  • 希望达到跟 vue 一样,在 js 配置中则完成路由重定向的等基础操作,不太习惯使用 Routes、Route 等互相包裹的方式。

  • 所有基于 react-router-dom@6.15.0 封装了一个路由组件,并附带展示个路由守卫组件。

  • 路由组件 - ExRouter.tsx<ExRouter routes={routes}></ExRouter>

    扩展路由配置,支持重定向,以及方便扩展其他属性。

    ```js // 基于 react-router-dom@6.15.0 封装 import { useRoutes, Navigate, IndexRouteObject, NonIndexRouteObject } from 'react-router-dom' import { useLocation, Location } from 'react-router'

    /**

    • @description: 扩展 IndexRouteObject / interface exIndexRouteObject extends IndexRouteObject { /*
      • @description: 重定向路由地址 */ redirect?: string }

    /**

    • @description: 扩展 NonIndexRouteObject / interface exNonIndexRouteObject extends NonIndexRouteObject { /*
      • @description: 重定向路由地址 */ redirect?: string }

    /**

    • @description: 路由对象类型 / export type exRouteObject = exIndexRouteObject | exNonIndexRouteObject /*
    • @description: 找到路由对象类型 */ export type findExRouteObject = exRouteObject | undefined

    /**

    • @description: 组件参数 / type props = { /*
      • @description: 路由列表 / routes: exRouteObject[], /*
      • @description: 子组件列表 */ children?: any }

    const Component = (props: props) => { // 当前导航对象 const location = useLocation() // 找到路由对象 const findRoute = (routes: exRouteObject[], location: Location): findExRouteObject => { // 当前层级检查一轮 let route: any = routes.find((item: any) => item.path === location.pathname) // 没有则搜索当前层级同级子页面 if (!route) { // 排查,找到停止 routes.some((item: any) => { // 取出子列表 const children: exRouteObject[] = item?.children || [] // 进行排查 route = findRoute(children, location) // 有值则暂停 return !!route }) } // 返回 return route } // 找到当前路由 const route: findExRouteObject = findRoute(props.routes, location) // 返回渲染 return ( <> {/* 加载所有路由 /} { useRoutes(props.routes) } {/ 检查当前路由是否需要重定向 */} { route?.redirect && } > ) }

    export default 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会尽力回答你的问题。 首先,我们需要了解Vue.js是什么。Vue.js是一个轻量级的JavaScript框架,用于构建交互式的Web界面。它与Angular和React类似,但相比之下,Vue.js更加简单易用,因此被广泛应用于Web开发中。 接下来,我们可以通过Vue.js提供的各种API和路由来实现用户登录注册程序设计。具体步骤如下: 1. 引入Vue.jsVue Router 首先,在HTML文件中引入Vue.jsVue Router: ``` <script src="https://cdn.jsdelivr.net/npm/vue"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> ``` 2. 创建Vue应用 接下来,我们需要创建一个Vue应用: ``` const app = new Vue({ el: '#app', router: router, data: { // ... }, methods: { // ... } }) ``` 在这个应用中,我们使用Vue Router,它是Vue.js提供的一个路由管理器,用于管理应用的不同页面。 3. 创建登录和注册组件 接下来,我们需要创建登录和注册组件。这些组件将用于在应用中显示登录和注册表单。 ``` const LoginComponent = { template: ` <div> <h2>登录</h2> <form> // ... </form> </div> ` } const RegisterComponent = { template: ` <div> <h2>注册</h2> <form> // ... </form> </div> ` } ``` 在这些组件中,我们可以编写表单代码,用于收集用户的登录和注册信息。 4. 定义路由 接下来,我们需要定义路由路由将用于将URL映射到不同的组件上。 ``` const router = new VueRouter({ routes: [ { path: '/', redirect: '/login' }, { path: '/login', component: LoginComponent }, { path: '/register', component: RegisterComponent } ] }) ``` 在这个路由定义中,我们将根URL重定向到登录页面,然后定义了两个路由,一个用于登录,一个用于注册。 5. 在应用中使用路由 最后,我们需要在应用中使用路由。我们可以使用Vue.js提供的`<router-view>`标签来显示当前路由的组件。 ``` <div id="app"> <router-view></router-view> </div> ``` 现在,我们已经完成了用户登录注册程序的设计。用户可以通过浏览器访问/login和/register URL来访问登录和注册页面,然后填写表单进行登录和注册。 以上就是使用Vue.js各种API及路由实现用户登录注册程序设计的步骤。希望能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡尔特斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值