Vue自定義指令實(shí)現(xiàn)按鈕級別權(quán)限

一但狭、src文件夾下新建utils文件夾披诗,文件夾下新建authoruty.js文件:

/**
 * 檢查權(quán)限點(diǎn)工具函數(shù)
 * @param {*} permissionCode 元素權(quán)限點(diǎn)
 * @param {*} permissions 所有權(quán)限點(diǎn)
 */
export function checkAuthority(permissionCode, permissions) {
  let hasPermission = true;
  
  if (permissionCode) {
    if (permissionCode instanceof Array && permissionCode.length > 0) {
      hasPermission = permissions.some(it =>
        permissionCode.includes(it)

      );
      console.log('permissionCode---', permissionCode)
      console.log('hasPermission---1', hasPermission)
    } else {
      hasPermission = permissions.some(item => item === permissionCode);
      console.log('hasPermission---2', hasPermission)
    }
  }
  return hasPermission;
}

二、src文件下新建directive文件夾立磁,隨之再建立Authority文件夾呈队,并在其下新建Authority.js文件和index.js文件,

  1. Authority.js文件代碼:
import store from "@/store";
import { checkAuthority } from "@/utils/authority";
// console.log('store-----', store.state.user.permission)
console.log("store-----", store);
export default {
  inserted(el, binding, vnode) {
    const { value } = binding;
    const permissions = JSON.parse(localStorage.getItem('permissionData')) || []
    console.log('permissionList---', permissions)
    const hasPermission = checkAuthority(value, permissions);
    if (!hasPermission) {
      // eslint-disable-next-line no-unused-expressions
      if (el.parentNode) {
        el.parentNode.removeChild(el);
      } else {
        el.innerHTML = "";
      }
    } else {
      el && el.setAttribute("code", value);
    }
  },
};
  1. index.js文件代碼:
import authority from './Authority';
// eslint-disable-next-line func-names
const install = function(Vue) {
  Vue.directive('authority', authority);
};

if (window.Vue) {
  window.authority = authority;
  Vue.use(install); // eslint-disable-line
}

authority.install = install;
export default authority;

(備注: 權(quán)限點(diǎn)集合permissionData在登錄時(shí)已經(jīng)存到localStorage里面了唱歧,取值宪摧,直接JSON.parse(localStorage.getItem('permissionData')))
三粒竖、main.js文件里引入:

import AuthorityDirective from '@/directive/Authority/index.js';  // 自定義指令
Vue.use(AuthorityDirective);

四、使用:

<el-button type="primary" v-authority="['Table_Write']">show</el-button>

補(bǔ)充:
權(quán)限點(diǎn)集合是一個(gè)純數(shù)組集合几于,該數(shù)據(jù)模擬從后臺(tái)獲取到的數(shù)據(jù):

permissionData = ['Table_Write', 'Table_Read', 'Form_Write', 'Form_Read']

獲取到后蕊苗,在登錄時(shí)立即存入localStorage。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末沿彭,一起剝皮案震驚了整個(gè)濱河市朽砰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌喉刘,老刑警劉巖瞧柔,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異睦裳,居然都是意外死亡造锅,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門廉邑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來备绽,“玉大人,你說我怎么就攤上這事鬓催》嗡兀” “怎么了?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵宇驾,是天一觀的道長倍靡。 經(jīng)常有香客問我,道長课舍,這世上最難降的妖魔是什么塌西? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮筝尾,結(jié)果婚禮上捡需,老公的妹妹穿的比我還像新娘。我一直安慰自己筹淫,他們只是感情好站辉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著损姜,像睡著了一般饰剥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上摧阅,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天汰蓉,我揣著相機(jī)與錄音,去河邊找鬼棒卷。 笑死顾孽,一個(gè)胖子當(dāng)著我的面吹牛祝钢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播若厚,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼太颤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了盹沈?” 一聲冷哼從身側(cè)響起龄章,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎乞封,沒想到半個(gè)月后做裙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡肃晚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年锚贱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片关串。...
    茶點(diǎn)故事閱讀 40,001評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拧廊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晋修,到底是詐尸還是另有隱情吧碾,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布墓卦,位于F島的核電站倦春,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏落剪。R本人自食惡果不足惜睁本,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望忠怖。 院中可真熱鬧呢堰,春花似錦、人聲如沸凡泣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽问麸。三九已至往衷,卻和暖如春钞翔,著一層夾襖步出監(jiān)牢的瞬間严卖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工布轿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留哮笆,地道東北人来颤。 一個(gè)月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像稠肘,于是被迫代替她去往敵國和親福铅。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評論 2 355