vue权限指令

权限是两个参数控制所以返回拿到的值用split分割字符串来做判断,权限检查方法里的判断具体根据自己的代码进行调整

main.js

/**权限指令**/
Vue.directive('has', {
  bind: function (el, binding) {
    if (!Vue.prototype.$_has(binding.value)) {
      el.style.display = "none"
    }
  }
});
//权限检查方法
Vue.prototype.$_has = function (value) {
  let str = value.split(',');
  let isExist = false;
  let buttonPermsStr = localStorage.getItem("buttonPermsStr");
  if (buttonPermsStr == undefined || buttonPermsStr == null) {
    return false;
  }
  let buttonPerms = JSON.parse(buttonPermsStr);
  for (let i = 0; i < buttonPerms.length; i++) {
    if (buttonPerms[i].permissionStr == str[0] || buttonPerms[i].permissionStr == str[1]) {
      isExist = true;
       break;
    }
  }
  return isExist;
};

  index.vue

v-has="'crm:contract:*,crm:contract:create'"

 

posted @ 2020-12-16 09:51  小凢  阅读(378)  评论(0编辑  收藏  举报