Ant Design Pro 使用Authorized组件做权限验证

Ant Design Pro做为阿里旗下的开源前端框架其优秀和优势就不用赘述了。
请看下图

今天我们要讲的是如何在ant design pro中做权限验证。

先上代码:

import RenderAuthorized from '../components/Authorized';

const user=getUser();
const Authorized = RenderAuthorized(user.role);
const noMatch = <Alert message="No permission." type="error" showIcon />;


const havePermission = () => {
  return false;
};

const havePermissionAsync = new Promise((resolve,reject)=>{
  // Call reslove on behalf of passed
  setTimeout(()=>reslove(),1000)
});

ReactDOM.render(
  <div>
    <Authorized authority="admin" noMatch={noMatch}>
      <Alert message="user Passed!" type="success" showIcon />
    </Authorized>
    <Authorized authority={['user','admin']} noMatch={noMatch}>
      <Alert message="Use Array as a parameter passed!" type="success" showIcon />
    </Authorized>
    <Authorized authority={Havepermission} noMatch={noMatch}>
      <Alert message="Use function as a parameter passed!" type="success" showIcon />
    </Authorized>
    <Authorized authority={havePermissionAsync} noMatch={noMatch}>
      <Alert message="Use Promise as a parameter passed!" type="success" showIcon />
    </Authorized>
  </div>
  mountNode,
);


现在我们再回头分析代码。

Authorized组件的authority属性支持:

1、 字符串。应用场景为当你只想一个角色有权限使用该组件的时候。
2、数组。 应用场景为当你想多个角色有权限使用该组件的时候。
3、函数。当你的业务需要使用函数的时候使用。
4、 Promise 。当你需要使用promise的时候使用。



Authorized组件的noMatch属性是支持嵌入其它组件的,这样你可以放提示信息的组件,甚至是跳转到登陆页的Redirect组件像这样:
const noMatch = <Redirect exact from="/" to="/user/login"/>;

猜你喜欢

转载自blog.csdn.net/parseserver/article/details/79026716
今日推荐