core icon indicating copy to clipboard operation
core copied to clipboard

Custom Directive with removal of component

Open rationalthinker1 opened this issue 2 years ago • 4 comments

What problem does this feature solve?

I'm writing a custom directive in vue.

I want it to work like v-if but it will have a little logic going inside it. Let me explain with an example:

<button v-permission-show="PermissionFoo">Do Foo</button>

It will check the permission and will keep or remove the component.

app.directive('permission-show', {
  mounted(el, binding, _vnode) {
    const permissionName = binding.value;
    const defaultValue = binding.value || false;

    const permission = Permission.get(permissionName, defaultValue);
    if (!permission) {
      el.parentNode.removeChild(el);
    }
  },
});

This is what I'm currently using but it doesn't work in a lot of places.

What does the proposed API look like?

have an option in el similar to el.$destroy()

rationalthinker1 avatar Jul 27 '22 11:07 rationalthinker1

maybe like this, i'm not sure too.

imoprt { render } from "vue"
render(null, el);

pasBone avatar Jul 28 '22 03:07 pasBone

I tried that too. I get another error. Uncaught (in promise) TypeError: Cannot read properties of null (reading '_vnode')

app.directive('permission-show', {
  mounted(el, binding, _vnode) {
    const permissionName = binding.value;
    const defaultValue = binding.value || false;

    const permission = Permission.get(permissionName, defaultValue);
    if (!permission) {
       render(el, null);
    }
  },
});

rationalthinker1 avatar Aug 02 '22 00:08 rationalthinker1

I had the same problem. I expected to completely destroy the vnode in the beforeMount in directive, but I didn't find a way to do it @yyx990803

Autumn-one avatar Mar 31 '23 07:03 Autumn-one

I want to destroy the components manually

rockey2020 avatar Mar 15 '24 08:03 rockey2020