vue自定義權(quán)限指令的實(shí)現(xiàn)
定義v-hasPermi指令
/**
* v-hasPermi 操作權(quán)限處理
*/
import useUserStore from '@/store/modules/user'
export default {
mounted(el, binding, vnode) {
const { value } = binding
const all_permission = "*:*:*";
const permissions = useUserStore().permissions;
//permission為數(shù)組,在系統(tǒng)登錄后獲取保存至vueX中
if (value && value instanceof Array && value.length > 0) {
const permissionFlag = value
const hasPermissions = permissions.some(permission => {
return all_permission === permission || permissionFlag.includes(permission)
})
if (!hasPermissions) {
el.parentNode && el.parentNode.removeChild(el)
}
} else {
throw new Error(`請?jiān)O(shè)置操作權(quán)限標(biāo)簽值`)
}
}
}
接口返回的permissions的格式
permissions: [
"plan:planadd:add",
"plan:planadd:edit",
"performance:plan:add",
"performance:plan:edit",
"system:role:submit",
"performance:plan:list",
]注冊指令
在index.js文件中

import hasPermi from './permission/hasPermi'
export default function directive(app){
app.directive('hasPermi', hasPermi)
}
掛載安裝指令
/*
* main.js文件
*/
import { createApp } from 'vue'
import directive from './directive' // directive
const app = createApp(App)
directive(app)
在項(xiàng)目中使用
<el-button
type="primary"
@click="addTable(scope)"
:disabled="btnDis"
v-hasPermi="['deptManage:yearDispatch:add']"
>添加</el-button
>
到此這篇關(guān)于vue自定義權(quán)限指令的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue自定義權(quán)限內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
策略模式實(shí)現(xiàn) Vue 動(dòng)態(tài)表單驗(yàn)證的方法
策略模式(Strategy Pattern)又稱政策模式,其定義一系列的算法,把它們一個(gè)個(gè)封裝起來,并且使它們可以互相替換。封裝的策略算法一般是獨(dú)立的,策略模式根據(jù)輸入來調(diào)整采用哪個(gè)算法。這篇文章主要介紹了策略模式實(shí)現(xiàn) Vue 動(dòng)態(tài)表單驗(yàn)證,需要的朋友可以參考下2019-09-09
使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn)
這篇文章主要介紹了使用typescript構(gòu)建Vue應(yīng)用的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Vue.js構(gòu)建你的第一個(gè)包并在NPM上發(fā)布的方法步驟
這篇文章主要介紹了Vue.js構(gòu)建你的第一個(gè)包并在NPM上發(fā)布的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
vue-cli腳手架打包靜態(tài)資源請求出錯(cuò)的原因與解決
這篇文章主要給大家介紹了關(guān)于vue-cli腳手架打包靜態(tài)資源請求出錯(cuò)的原因與解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue-cli具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Elementui表格組件+sortablejs實(shí)現(xiàn)行拖拽排序的示例代碼
這篇文章主要介紹了Elementui表格組件+sortablejs實(shí)現(xiàn)行拖拽排序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

