假設某應用中有如下三個頁面:javascript
登錄頁----Home.jsx 頁面A----PageA.jsx 頁面B----PageB.jsx
有如下三種用戶:java
遊客(未登陸)---只能訪問登錄頁 普通用戶(已登錄)---能訪問登錄頁及頁面A 管理員(已登錄)---能訪問全部頁面
具體需求以下:react
因此首先將是否登錄做爲第一層權限:react-router
withUserPermission.jsx
代碼以下app
import React from 'react';
import { Redirect } from 'react-router-dom';
import { getUser } from '@/user.js'; // 判斷是否已登錄用戶
export default Wrapper => hocProps => {
const { component: Component, ...rest } = hocProps;
const user = getUser();
return (
<Wrapper {...rest} component={ props => {
return user
? <Component {...props} />
: <Redirect to="/login" /> // 跳轉到登錄頁
} } />
);
}
複製代碼
而後在第二層權限,判斷是否具備管理員權限: withAdminPermission.jsx
代碼以下dom
import React from 'react';
import { getRole } from '@/user.js'; // 獲取用戶角色
export default Wrapper => hocProps => {
const { component: Component, ...rest } = hocProps;
const hasPermission = (getRole() === 'admin') ;
return (
<Wrapper {...rest} component={ props => {
return hasPermission
? <Component {...props} />
: <h1>無權限訪問</h1>
} } />
);
}
複製代碼
使用這兩個高階組件,封裝Routespa
PrivateRoutes.js
代碼以下rest
import { Route } from 'react-router-dom';
import withUserPermission from './withUserPermission';
import withAdminPermission from './withAdminPermission';
export const UserRoute = withUserPermission(Route);
export const AdminRoute = withUserPermission(withAdminPermission(Route)); // 從外向內提高權限
複製代碼
下面能夠使用這兩個封裝好的Route組件,實現上面的需求:code
import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { UserRoute, AdminRoute } from './PrivateRoutes';
import PageLogin from 'views/PageLogin';
import PageA from 'views/PageA';
import PageB from 'views/PageB';
export default () => (
<Router>
<Route path='/login' component={PageLogin} />
<UserRoute path='/pageA' component={PageA} />
<AdminRoute path='/pageB' component={PageB} />
</Router>
);
複製代碼