mirror of
https://github.com/zclzone/vue-naive-admin.git
synced 2026-01-22 23:50:22 +08:00
39 lines
1.0 KiB
JavaScript
39 lines
1.0 KiB
JavaScript
/**********************************
|
||
* @Author: Ronnie Zhang
|
||
* @LastEditor: Ronnie Zhang
|
||
* @LastEditTime: 2023/12/05 21:23:01
|
||
* @Email: zclzone@outlook.com
|
||
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
|
||
**********************************/
|
||
|
||
import { router } from '@/router'
|
||
import { withDirectives } from 'vue'
|
||
|
||
const permission = {
|
||
mounted(el, binding) {
|
||
const currentRoute = unref(router.currentRoute)
|
||
const btns = currentRoute.meta?.btns?.map(item => item.code) || []
|
||
if (!btns.includes(binding.value)) {
|
||
el.remove()
|
||
}
|
||
},
|
||
}
|
||
|
||
export function setupDirectives(app) {
|
||
app.directive('permission', permission)
|
||
}
|
||
|
||
/**
|
||
* 用于h函数使用自定义权限指令
|
||
*
|
||
* @param {*} vnode 虚拟节点
|
||
* @param {*} code 权限码
|
||
* @returns 返回一个包含权限指令的vnode
|
||
*
|
||
* 使用示例:withPermission(h('button', {class: 'text-red-500'}, '删除'), 'user:delete')
|
||
*
|
||
*/
|
||
export function withPermission(vnode, code) {
|
||
return withDirectives(vnode, [[permission, code]])
|
||
}
|