Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove element in vue directive

I want to create a v-if like directive, but i can't find a way to remove element so i hide the element like this..

<Button v-check="'aaa'" type="primary">aaa</Button>
<Button v-check="'bbb'" type="primary">bbb</Button>
Vue.directive('check', {
    bind(el, binding, vnode, old) {
        if (binding.value === 'aaa') {
            el.style.display = 'none'
        }
    }
})

i want to remove the element totally Is there any way that i can remove the element?

like image 873
frankw Avatar asked Dec 10 '17 03:12

frankw


People also ask

How do you remove an item from Vue?

To remove an element from a list with Vue. js, we can use the JavaScript array splice method. to add the removeElement method that takes the index of the item we want to remove from the this. items array.

How do you delete an element from an array Vue?

Deleting elements in an array maintains reactivity in Vue when done right. These arrays can be local variables in a component, or state variables in Vuex - the behaviour is the same. We just use the standard pop and shift to delete elements in the array.


1 Answers

ok, i find a way

Vue.directive('check', {
  inserted(el, binding, vnode, old) {
    if (binding.value === 'aaa') {
      vnode.elm.parentElement.removeChild(vnode.elm)
    }
  }
})
like image 114
frankw Avatar answered Oct 09 '22 21:10

frankw