I’m creating my own role/rights system because the ones that are available for Vue don’t work.
I now hide the element when a user does not have the appropriate role. But what I actually want is to not render the component at all. But I don’t know how to achieve that.
I have the following code:
app.directive('hasRole', hasRole)
import {useUserStore} from "@/stores/UserStore.js";
export default {
// called before bound element's attributes
// or event listeners are applied
created(el, binding, vnode, prevVnode) {
//console.log(el, binding, vnode, prevVnode)
},
// called right before the element is inserted into the DOM.
async beforeMount(el, binding, vnode, prevVnode) {
const userStore = useUserStore()
await userStore.fill()
console.log(userStore.getUser.roles.includes(binding.value))
if (!userStore.getUser.roles.includes(binding.value)) {
// el.style.display = 'none'; <---- this hides the element, so that works.
vnode = null
return el = null;
el.style.display = 'none';
}
},
// called when the bound element's parent component
// and all its children are mounted.
mounted(el, binding, vnode, prevVnode) {
//console.log(el, binding, vnode, prevVnode)
},
// called before the parent component is updated
beforeUpdate(el, binding, vnode, prevVnode) {
//console.log(el, binding, vnode, prevVnode)
},
// called after the parent component and
// all of its children have updated
updated(el, binding, vnode, prevVnode) {
//console.log(el, binding, vnode, prevVnode)
},
// called before the parent component is unmounted
beforeUnmount(el, binding, vnode, prevVnode) {
//console.log(el, binding, vnode, prevVnode)
},
// called when the parent component is unmounted
unmounted(el, binding, vnode, prevVnode) {
//console.log(el, binding, vnode, prevVnode)
}
}
How can I prevent the render of the component?
3
Answers
I think that using directives for this purpose, is not the right way, in order to avoid component rendering.
To avoid a component to be rendered you should, insted, use the
v-if
condition.To share the logic of
hasRole
between your component, you can use mixins, or just write a small plugin you can add to Vue app.Here is the corrected code I have.
You already have a user store that can be injected into any component.
I suggest computing often-used keys like
isAdmin
,isSuperAdmin
in the store:In your components, you can use the following pattern to conditionally render items:
This circumvents direct DOM manipulation, which is always a pain and often a bad practice.