Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Vue custom directive should replace the element with a comment but child component mounted method gets called first

We developed a Vue custom directive that replaces DOM elements with a comment depending if the user has permission for specific information. This directive works fine when we use it in any DOM element, but when we apply it to another Vue component it doesn't work as expected because the mounted() function gets called, which we don't want

The problem really is that we don't want to render that component, specifically if it has an API call because this makes the whole system fail.

Here is the directive code:

Vue.directive("checkPermission", {
    bind: (el, binded, vnode) => {
        let permissions = self.$nuxt.$store.$auth.user.permissions;

        if (
            !self.$nuxt.$store.$auth.user.isAdmin &&
            permissions &&
            permissions.length > 0
            ) {
            let hasPermission = false;
            const { action, subject } = binded.value;

            hasPermission = permissions.some(
                permmission =>
                    permmission.action === action &&
                    (Array.isArray(subject) ? subject.includes(permmission.subject) : permmission.subject === subject)
            );

            if (!hasPermission) {
                const comment = document.createComment(" ");

                Object.defineProperty(comment, "setAttribute", {
                    value: () => undefined
                });

                vnode.text = " ";
                vnode.elm = comment;
                vnode.isComment = true;
                vnode.context = undefined;
                vnode.tag = undefined;
                vnode.data.directives = undefined;
                vnode.children = undefined

                if (vnode.componentInstance) {
                    vnode.componentInstance.$el = comment;
                }

                if (el.parentNode) {
                    el.parentNode.replaceChild(comment, el);
                }
            }
        }
    },
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!