Vue技术

vue自定义指令使用场景,哪些钩子函数、各是什么意思?

2021-06-22  本文已影响0人  源大侠

自定义指令使用场景

自定义指令使用实例

const auths=["order","users","activity"];
const isAuth=(current)=>{
    const roles=localStorage.getItem("auths");
    const isR=current.some(item => auths.includes(item));
    return isR;
}

Vue.directive('auths', {
  //01
  bind: function (el, binding, vnode) {
  },
  //02
  inserted: function (el, binding) {
    const current  = binding.value
      if(!current||!isAuth(current)){
        el.parentNode && el.parentNode.removeChild(el);
      }
  },
  //03
  update: function (el, binding, vnode,oldVnode) {
  },
  //04
  componentUpdated: function (el, binding, vnode,oldVnode) {
  },
  //05
  unbind: function () {
  },
})
<template>
  <a v-auths="['order']">编辑订单</a>
</template>

触发顺序

如auths自定义指令中定义的顺序

自定义指令钩子函数

上一篇 下一篇

猜你喜欢

热点阅读