MCPcopy Index your code
hub / github.com/NSFI/react-router-manage

github.com/NSFI/react-router-manage @v2.0.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.0.4 ↗ · + Follow
942 symbols 2,645 edges 188 files 3 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

react-router-manage

Downloads per month on npm. Total downloads on npm.

简体中文 | English

功能简介

react-router-manage基于react-router v6版本实现,通过配置可实现路由的鉴权、路由守卫、路由的增删改查等功能。由于react-router v5升级到v6有较大的成本,react-router-manage提供了原来 v5 部分的 api 用于兼容使用 v5 的项目,用于平滑升级 v6

  • 🛠 [config router] - 集中配置路由,快捷、方便管理。
  • + [addRoutes] - 动态增加路由:可使用 hookuseAddRoutes添加路由,自动刷新视图。
  • ➖ [removeRoutes] - 动态删除路由:可使用 hookuseRemoveRoutes删除路由,自动刷新视图。
  • 🖇 [updateRoutes] - 动态修改路由:可使用 hookuseUpdateRoutes修改路由。
  • 🔐 [permission] - 权限控制:配置路由的 code,自动管理路由的权限
  • 👨‍✈️‍ [Route guard] - 提供路由进入时的回调 beforeEnter and beforeEachMount, 路由离开时的钩子 useBeforeLeave
  • 🌲 [navigation] - 层级导航:支持层级导航,自动实现父子级路由的导航栏生成,例如面包屑、菜单导航
  • 基于 antd 自动生成的面包屑导航 antd-breadcrumbs

安装


npm install react-router-manage --save

示例

配置项

defineRouterConfig(routerConfig)

路由的全局配置

字段名 说明 类型 是否必填
basename 路由的路由前缀 string 非必填,默认 /
routes 路由的层级配置 RouteTypeI[] 必填
beforeEachMount 每个路由在渲染前调用 (to: RouteTypeI \| undefined, next: ({path?: string; name: string} \| React.ComponentType<any>) => void): void 非必填
autoDocumentTitle 文档的 title 会根据路由切换而改变 boolean | (RouteTypeI[]) => string 非必填, 默认 false
LoadingComponent 用于 Suspense 加载异步组件时配置 fallback 或在有 beforeEnter 钩子的next时,显示加载中 React.FunctionComponent

defineRouterConfig 返回额外属性

| 字段名 | 说明 | 类型 | 是否必填 | | ---------- | ------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- | -------- | -------------- | | navigate | 用于在Router外进行跳转路由,需初始化 <Router /> 后可调用 | (to: string, {query: Record<string, any>; params: Record<string, any>; state: any}) => void} | | not required |

路由模式

路由模式目前有两种

  • history模式
  • hash模式

一个简单的全局配置(history模式

import React from 'react';
import { MRouter, defineRouterConfig } from 'react-router-manage';

const routerConfig = defineRouterConfig({
  basename: '/',
  routes: [{...}], // 请查看下方路由配置 routes
  // autoDocumentTitle: true, // 设置true,会自动设置变换document.title,
  // autoDocumentTitle: (routes) => return `网易云商-${routes.map((i) => i.title).join('-')}`, // 自定已配置document.title的设置

  // beforeEachMount: (to, next) => { // 配置全局的路由进入守卫,可查看下方全局路由守卫介绍
  //   console.log(to)
  //   next();
  // }
})

function App () {
  return (
    <MRouter routeConfig={routeConfig}>
      {(children) => children}
    </MRouter>
  )
}

一个简单的全局配置(hash模式

import React from 'react';
import { MHRouter, defineRouterConfig } from 'react-router-manage';

const routerConfig = defineRouterConfig({
  basename: '/',
  routes: [{...}], // 请查看下方路由配置 routes
  // autoDocumentTitle: true, // 设置true,会自动设置变换document.title,
  // autoDocumentTitle: (routes) => return `网易云商-${routes.map((i) => i.title).join('-')}`, // 自定已配置document.title的设置

  // beforeEachMount: (to, next) => { // 配置全局的路由进入守卫,可查看下方全局路由守卫介绍
  //   console.log(to)
  //   next();
  // }
})

function App () {
  return (
    <MHRouter routeConfig={routeConfig}>
      {(children) => children}
    </MHRouter>
  )
}

路由配置 routes

字段名 说明 类型 是否必填
name 路由的名称, 名称全局唯一、不能重复,用于获取路由 string 必填
path 路由的路径,组合后的完整路径全局唯一、不能重复,但是如果是嵌套的子路由,可以不配置, 相当于Route组件中设置index属性 string 必填
title 路由的中文名称,显示的名称,用于自动生成导航和面包屑中 string 非必填
index 同级有多个路由,会找带 index 的路由作为进入的路由 boolean 非必填
component 路由匹配的组件, 如果没有配置,则会跳到下一级有权限的路由 React.Component | React.FunctionComponent 非必填
items 视觉上的子级路由,用于导航时的父子级关系,实际为同一级路由 RouteTypeI[] 非必填
children 子级路由、在 v6 版本中渲染在 Outlet组件中 RouteTypeI[] 非必填
props 渲染组建时候会自动注入 Props 里面的内容, <Component {...props}/> Record<string, any> 非必填
hidden 导航的显示与隐藏 boolean 非必填, 默认 false
code 用于权限校验,会对比permissionList里的值 string| string[]| (route: RouteTypeI) => boolean 非必填,默认无
redirect 路由重定向到指定路由,优先级高于 component string 非必填,默认无
beforeEnter 渲染该路由钱调用的方法,如果调用next中传入了组件,则会渲染该组件,路由配置的组件则不会渲染 (to: RouteTypeI \| undefined, next: (options?: {name?: string; path?: string} \| React.ComponentType<any>) => void): void 非必填, 默认无
beforeLeave 离开路由前调用的回调,需主动调用next (to: RouteTypeI \| undefined,from: RouteTypeI \| undefined, next: () => void): void 非必填
meta 一些自定义的信息可以放这里,currentRoute.meta可以获取到该字段 Record<string, any> 非必填
fullscreen 是否全屏,在base-layout-router中监测到当前route fullscreentrue,则会隐藏导航栏 boolean 非必填,默认无
icon 用于显示导航的 icon string 非必填, 默认无
type 如果typenull字符串,则此路由不会真正渲染,但是可以设置正确的 currentRoute real | null 非必填,默认 real
breadcrumbs 用于配置路由中面包屑的配置, antd-breadcrumbs BreadcrumbsI 非必填

BreadcrumbsI

字段名 说明 类型 是否必填
isRoot 是否是面包屑的根节点,如果是,则从下一级开始算 boolean false
text 面包屑的名称,如果不配置,则默认使用route.title string | React.ReactNode | (route: RouteTypeI) => React.ReactNode 非必填
hidden 是否隐藏本级面包屑显示 boolean false

注意事项

  • 如果在code里配置了一个函数,由于在路由初始化会批量调用,请不要进行异步调用,如果需要建议使用beforeEnter达到同样效果
  • 如果父级路由没有配置 component, 跳转到该路由则会寻找 items,children 下第一个有权限的路由,若找不到,则会显示无权限页面
  • 如果redirectcomponent同时进行了配置,则component会被忽略
  • beforeEnterbeforeEachMountnext可传入一个组件,若传入则会渲染该组件, 如果在 react 使用严格模式,则函数可能会调用两次,这个是正常情况

items 与 children

通过 ys-router,你可以使用children, items配置来表达路由导航的父子关系。

items 同一级别的路由,父级与子级渲染一个

/**
 *  文章列表页和文章详情页在不同的页面
 *  /user/article/list  文章列表页面
 *  /user/article/detail 文章详情页
 * */
/user/article/list                     /user/article/detail
+------------------+                  +-----------------+
| +--------------+ |                  | +-------------+ |
| | ------------ | |  +------------>  | | content     | |
| | ------------ | |                  | |             | |
| | ------------ | |                  | |             | |
| | ------------ | |                  | |             | |
| +--------------+ |                  | +-------------+ |
+------------------+                  +-----------------+

一个有基本路由配置的示例

```tsx import React from "react"; import { MRouter, defineRouterConfig } from "react-router-manage";

const Users = () => { return

Users

; }; const Profile = () => { return <

Extension points exported contracts — how you extend this code

ChangeableI (Interface)
* LoadingComponent Define the Suspense component's fallback and beforeEnter when a component is loaded, * and the state
packages/react-router-manage/src/changeable.tsx
RouterBreadcrumbsProps (Interface)
(no doc)
packages/breadcrumbs/index.tsx
AntdRouterBreadcrumbsProps (Interface)
(no doc)
packages/antd-breadcrumbs/index.tsx
RouterBreadcrumbsProps (Interface)
(no doc)
packages/keep-alive/index.tsx
Window (Interface)
(no doc)
types/index.d.ts
Window (Interface)
(no doc)
examples/custom-link/src/vite-env.d.ts
Window (Interface)
(no doc)
examples/beforeEachMount/src/vite-env.d.ts
Window (Interface)
(no doc)
examples/auth/src/vite-env.d.ts

Core symbols most depended-on inside this repo

n
called by 279
docs/assets/js/main.e3a6ce89.js
push
called by 156
packages/react-router-manage/src/type.ts
n
called by 156
docs/assets/js/210.6e5e9f1a.js
a
called by 152
docs/assets/js/210.6e5e9f1a.js
i
called by 127
docs/assets/js/main.e3a6ce89.js
o
called by 82
docs/assets/js/main.e3a6ce89.js
f
called by 78
docs/assets/js/main.e3a6ce89.js
replace
called by 75
packages/react-router-manage/src/type.ts

Shape

Function 851
Interface 71
Method 12
Class 6
Enum 2

Languages

TypeScript100%

Modules by API surface

docs/assets/js/main.e3a6ce89.js361 symbols
packages/react-router-manage/src/type.ts38 symbols
docs/assets/js/210.6e5e9f1a.js32 symbols
packages/react-router-manage/src/util/index.tsx29 symbols
docs/assets/js/17896441.e80a4204.js25 symbols
docs/assets/js/1be78505.75c6ac2b.js23 symbols
docs/assets/js/2529.25dca86e.js20 symbols
docs/assets/js/14eb3368.f3610a8f.js14 symbols
examples/auth/src/App.tsx12 symbols
docs/assets/js/ccc49370.d9c41d1e.js11 symbols
examples/children/src/App.tsx9 symbols
examples/beforeEachMount/src/App.tsx8 symbols

For agents

$ claude mcp add react-router-manage \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact