Reference VUE-Element-ADMIN permission for the libraries
Use Demo (demo.vue)
<template> <p>admin组:<span v-permission="['admin']">可看见v-permission="['admin']"</span></p> <p>editor:<span v-permission="['editor']">可看见v-permission="['editor']"</span></p> <p>admin editor组:<span v-permission="['admin','editor']">可看见v-permission="['admin','editor']"</span></p> <p>another组:<span v-permission="['another']">可看见v-permission="['another']"</span></p> </template>
v-permission instruction code to a global register (main.js)
const permission={ inserted(el, binding, vnode) { const { value } = binding const roles = ['editor','admin'] if (value && value instanceof Array && value.length > 0) { const permissionRoles = value const hasPermission = roles.some(role => { return permissionRoles.includes(role) }) if (!hasPermission) { el.parentNode && el.parentNode.removeChild(el) } } else { throw new Error(`need roles! Like v-permission="['admin','editor']"`) } } } Vue.directive('permission', permission)
Compared
- v-show (v-show of dom does not hide, change the display the user can see the information not supposed to see)
- v-if (v-if left to delete nodes <! - -> comments)
Easy to maintain, and clear