Ant Design Pro 使用Authorized元件做許可權驗證
阿新 • • 發佈:2019-01-28
Ant Design Pro做為阿里旗下的開源前端框架其優秀和優勢就不用贅述了。
請看下圖
今天我們要講的是如何在ant design pro中做許可權驗證。
Authorized元件的authority屬性支援:
1、字串。應用場景為當你只想一個角色有許可權使用該元件的時候。
2、陣列。應用場景為當你想多個角色有許可權使用該元件的時候。
3、函式。當你的業務需要使用函式的時候使用。
4、Promise 。當你需要使用promise的時候使用。
Authorized元件的noMatch屬性是支援嵌入其它元件的,這樣你可以放提示資訊的元件,甚至是跳轉到登陸頁的Redirect元件像這樣:
const noMatch = <Redirect exact from="/" to="/user/login"/>;
請看下圖
今天我們要講的是如何在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"/>;