vue自定义指令记录

auth.js


export function check(authority) {
    let userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
    let current = userInfo ? userInfo.buttons: [];
    return current.some(item => authority.includes(item)); //过滤
}

index.js

import { check } from "./auth";
// 开发插件的方式;定义指令;第一个参数是Vue 构造器,第二个参数是可选的选项对象
function install(Vue, options = {}) {

  Vue.directive(options.name || "auth", {
    // 指令定义对象的钩子函数inserted
    inserted(el, binding) {
      if (!check(binding.value)) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    }
  });
}

export default { install };

全局注入

//通过全局方法 Vue.use() 使用插件。它需要在你调用 new Vue() 启动应用之前完成:
import Auth from "@/directives/index";
Vue.use(Auth);

页面使用

 <Button type="info" icon="md-add" class="mini-btn" @click="handleSave" v-auth="['system:user:add']">拉取用户</Button>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值