react?路由權(quán)限動(dòng)態(tài)菜單方案配置react-router-auth-plus
正文
在 react 中做路由權(quán)限管理,一直是比較麻煩的事,不像 vue 中有進(jìn)入路由前攔截的功能。在摸魚時(shí)間擼了一個(gè)傻瓜式配置的路由權(quán)限 library (基于 react-router v6)。
react-router-auth-plus github 地址
如何使用
1. 配置路由
import { AuthRouterObject } from "react-router-auth-plus"; const routers: AuthRouterObject[] = [ { path: "/", element: <Navigate to="/home" replace /> }, { path: "/login", element: <Login /> }, { element: <Layout />, children: [ { path: "/home", element: <Home />, auth: ["admin"] }, { path: "/setting", element: <Setting /> }, { path: "/application", element: <Application />, auth: ["application"], }, ], }, { path: "*", element: <NotFound /> }, ];
2. 在應(yīng)用的最外層渲染路由
這里我使用 swr 來模擬獲取當(dāng)前用戶的權(quán)限,兩秒后返回。
// App.tsx import { useAuthRouters } from "react-router-auth-plus"; const fetcher = async (url: string): Promise<string[]> => await new Promise((resolve) => { setTimeout(() => { resolve(["admin"]); }, 2000); }); function App() { const { data: auth, isValidating } = useSWR("/api/user", fetcher, { revalidateOnFocus: false, }); return useAuthRouters({ // 當(dāng)前用戶的權(quán)限,string[] auth: auth || [], routers, // 跳轉(zhuǎn)到?jīng)]權(quán)限的路由時(shí),用戶自定義顯示。這里我顯示 403 頁(yè)面。 noAuthElement: (router) => <NotAuth />, // 用戶權(quán)限還沒請(qǐng)求到時(shí),渲染 loading render: (element) => (isValidating ? element : <Loading />), }); }
或你可以使用 jsx 的方式來配置
import { AuthRoute, createAuthRoutesFromChildren } from "react-router-auth-plus"; useAuthRouters({ auth: auth || [], noAuthElement: (router) => <NotAuth />, render: (element) => (isValidating ? element : <Loading />), routers: createAuthRoutesFromChildren( <Routes> <AuthRoute path="/" element={<Navigate to="/home" replace />} /> <AuthRoute path="/login" element={<Login />} /> <AuthRoute element={<Layout />}> <AuthRoute path="/home" element={<Home />} auth={["admin"]} /> <AuthRoute path="/setting" element={<Setting />} /> <AuthRoute path="/application" element={<Application />} auth={["application"]} /> </AuthRoute> <AuthRoute path="*" element={<NotFound />} /> </Routes> ), });
這樣就完成了,是不是很傻瓜式呢?
權(quán)限說明
若當(dāng)前 home 的權(quán)限被設(shè)置為 ["auth1", "auth2", "auth3"]
,當(dāng)前用戶的權(quán)限只要滿足一個(gè)就會(huì)判斷為擁有此路由的權(quán)限。
動(dòng)態(tài)菜單
react-router-auth-plus
會(huì)自動(dòng)將 children 傳給 Layout,你不必在路由配置里傳給 Layout。如果你是 ts,將 routers 類型設(shè)置為可選即可。
useAuthMenus
會(huì)過濾掉沒有權(quán)限的路由,接下來你可以自行處理一下成你想要的數(shù)據(jù)再渲染成 antd 的 Menu 組件。
import { useAuthMenus, AuthRouterObject } from "react-router-auth-plus"; interface LayoutProps { routers?: AuthRouterObject; } const Layout:FC<LayoutProps> = ({ routers }) => { const menus = useAuthMenus(routers); ... }
以上就是react 路由權(quán)限動(dòng)態(tài)菜單方案配置react-router-auth-plus的詳細(xì)內(nèi)容,更多關(guān)于react 路由權(quán)限動(dòng)態(tài)菜單的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React中使用react-player 播放視頻或直播的方法
這篇文章主要介紹了React中使用react-player 播放視頻或直播,本文教大家如何使用react框架及創(chuàng)建實(shí)例的代碼,本文內(nèi)容簡(jiǎn)短給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01react18中react-redux狀態(tài)管理的實(shí)現(xiàn)
本文主要介紹了react18中react-redux狀態(tài)管理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05