如何删除vue指令中的元素

fra*_*nkw 6 javascript vue.js

我想创建一个类似 v-if 的指令,但我找不到删除元素的方法,所以我像这样隐藏了元素..

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

我想完全删除元素 有什么方法可以删除元素吗?

fra*_*nkw 8

好的,我找到了方法

Vue.directive('check', {
  inserted(el, binding, vnode, old) {
    if (binding.value === 'aaa') {
      vnode.elm.parentElement.removeChild(vnode.elm)
    }
  }
})
Run Code Online (Sandbox Code Playgroud)