如何用React-Router进行页面权限管理

前言

在一个复杂的SAP应用中,我们可能需要根据用户的角色控制用户进行页面的权限,甚至在用户进入系统之前就进行权限的控制。本文就此一权限控制进行讨论。本文假设读者了解React和React-Router的相关使用。

从传统的Router开始

一个传统的路由大概长下边这个样式,这是没有添加任何权限限制的。

export default (store) => { const history = syncHistoryWithStore(hashHistory, store); return ( <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router history={history}><<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="/"component={AppRoot} > <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">IndexRoute component={IndexPage} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="photo" component={PhotoPage} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="info" component={InfoPage} /> </<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route> {/ <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Redirect path="" to="/error" /> */}</<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router> ) }

这里一共有3个页面 IndexPage, PhotoPage,InfoPage。

我有几张阿里云幸运券分享给你,用券购买或者升级阿里云相应产品会有特惠惊喜哦!把想要买的产品的幸运券都领走吧!快下手,马上就要抢光了。

添加第一个权限

假设我们需要在用户进入PhotoPage之前需要验证用户是否有权限,根据store的的一个状态去判断。

先添加如下一个函数

const authRequired = (nextState, replace) => { // Now you can access the store object here. conststate = store.getState(); if (state.admin != 1) { replace('/'); } };

函数里我们判断了state的admin是否等于1,否则跳转到首页。

然后在Route添加 onEnter={authRequired} 属性

<<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="photo"component={PhotoPage} onEnter={authRequired} />

通过以上,就完成了第一个权限的添加

进入系统之前就进行权限控制

如果需要在进入系统之前就进行权限控制,那么就需要改变一下策略。
比如上边的例子,加入state的admin并未加载,那么就需要在上一层的route进行数据加载

首先添加一个加载数据的函数

function loadData(nextState, replace, callback) { let unsubscribe; function onStateChanged() { conststate = store.getState(); if (state.admin) { unsubscribe(); callback(); } } unsubscribe = store.subscribe(onStateChanged); store.dispatch(actions.queryAdmin()); }

接着再修改一下Router

<<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router history={history}> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Routepath="/" component={AppRoot} onEnter={loadData}> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">IndexRoute component={IndexPage} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="photo" component={PhotoPage} onEnter={authRequired} /> <<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route path="info" component={InfoPage} /> </<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Route> </<span class="hljs-name" style="box-sizing: border-box; color: rgb(102, 217, 239);">Router>

这样在进入下边之前,就会先进行数据加载。
通过以上简单几步,一个完整的权限控制链就完成了.

原文链接

转载于:https://my.oschina.net/u/3722616/blog/1575112

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Router 是一个用于在 React 应用中实现路由功能的库。它提供了一种声明式的方式来定义和管理应用程序的路由,使得页面之间的导航和状态管理更加方便和灵活。React Router 的原理可以概括为以下几个关键概念和步骤: 1. **路由器(Router):** React Router 提供了多种类型的路由器组件,如 `BrowserRouter`、`HashRouter` 等。路由器组件负责监听 URL 的变化,并将相应的路由信息传递给应用程序。 2. **路由规则(Route):** 使用 `Route` 组件来定义路由规则。每个 `Route` 组件负责匹配 URL,并在匹配成功时渲染对应的组件。可以通过 `path` 属性来指定匹配的路径,通过 `component` 属性来指定要渲染的组件。 3. **导航(Navigation):** React Router 提供了多种导航组件来实现页面之间的跳转,如 `Link`、`NavLink` 等。这些导航组件会生成对应的 `<a>` 标签,并处理点击事件来触发路由的变化。 4. **路由参数(Route Parameters):** 可以通过在路由规则中使用冒号(`:`)来定义动态的路由参数,如 `/users/:id`。在匹配成功后,可以通过 `props.match.params` 来获取路由参数的值。 5. **嵌套路由(Nested Routes):** React Router 支持嵌套路由,即在一个组件内部定义子组件的路由规则。可以通过嵌套的 `Route` 组件来实现。 6. **路由守卫(Route Guards):** React Router 提供了一些钩子函数,如 `beforeEnter`、`beforeLeave` 等,用于实现路由守卫功能。可以在路由跳转前或跳转后执行一些逻辑操作,例如验证用户权限、处理登录状态等。 总的来说,React Router 的原理是通过路由器监听 URL 的变化,根据定义的路由规则匹配对应的组件进行渲染,同时提供导航组件来实现页面之间的跳转。这样可以实现单页面应用(SPA)的路由功能,使得页面的切换和状态管理更加灵活和可控。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值