前端权限设计实现 (按钮等)
1.使用v-if
//store.state.authIds 为该用户所有的权限 格式为数组 ['home','admin']
<div v-if='authRequest("***")'> </div>
authRequest(auth) {
if (auth && !store.state.authIds.includes(auth)) {
return false;
} else {
return true;
}
},
2.使用指令
Vue.directive("auth", {
inserted: function (el, binding) {
if (binding.value && !store.state.authIds.includes(binding.value)) {
el.parentNode.removeChild(el)
}
}
});
<div v-auth="'***'"> </div>
//指令方式实现的权限控制,使用起来会比较简洁,但是是通过删减 dom 节点的方式实现的,因此只有在第一次时控制。
//指令 也可以在update 时候 去做处理
3.组件
<script>
const authRequest=(auth)=> {
if (auth && !store.state.authIds.includes(auth)) {
return false;
} else {
return true;
}
},
export default {
name: 'Authorized',
functional:true,
props: {
authority:{
type:Array,
required:true
}
},
render(h, context) {
const { props, scopedSlots } = context;
return authRequest(props.authority) ? scopedSlots.default() : null
},
}
</script>
<template>
<Authorized :authority="['admin']" >
<other-components></other-components>
</Authorized>
</template>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探