javascript - 如何删除vue指令中的元素

标签 javascript vue.js

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

<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'
        }
    }
})

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

最佳答案

好的,我有办法

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

关于javascript - 如何删除vue指令中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47735591/

相关文章:

javascript - 如何制作可调整大小的垂直处理程序?

javascript - do-while 语句

javascript - Vue.js应用中使用filter方法时的 "TypeError: Cannot convert undefined or null to object"

javascript - 如何在Hightcharts中设置垂直条形图|我作为组件通过了。如何作为 Prop 发送

javascript - Charts.js - 需要从工具提示中删除数据值

javascript - 你能在 jQuery 中使用三元运算符吗?

javascript - 同一 html 页面中的多个模式

javascript - 以编程方式在不同页面的 vue 组件中显示数据

node.js - 为 vue-cli 运行 "npm run dev"时出现跨环境问题

javascript - 在 vuejs 中使用 vuelidate 验证密码?