vue 自定义指令

131 阅读1分钟

自定义指令的使用

注册全局指令 Vue.directive()

  • 参数有2个,一个是自定义指令名称,另外一个是指令的属性
  Vue.directive('auth', {
    inserted: (el, { value }) => {
      if (!hasPermission(value)) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  })
  • 第二个参数接受的类型

1)inserted:表示元素插入到dom中时执行inserted方法

2)bind: 表示当指令绑定到元素上时执行bind方法,只会执行一次

3)update: 表示当window更新的时候执行,可能回执行多次

  • 使用

v-指令名称