vue.js自定义指令

1.在入口文件App.vue中定义

<template>
  <div id="app">
    <routerView/>
  </div>
</template>

<script>

import Vue from 'vue'

Vue.directive('has',{
    bind(el, binding, vnode) {
      if (!Vue.prototype.$_has(binding.value)) {
        vnode.context.$nextTick(() => el.parentNode.removeChild(el));
      }
    }
})
// 权限检查方法
  Vue.prototype.$_has = function(value) {
    if (!value) throw new Error('v-has方法参数不能为空');
    let authStr = window.localStorage.getItem('auth');
    if (authStr === undefined || authStr === null) {
      return false;
    }
    let authTable = authStr.split(',');
    if(authTable.some(v => v == value)){
      return true;
    }else{
      return false;
    }
  };

export default {
  created() {  
        this.setAuthInfo();
  },
  name: "App",
  computed: mapState(["Num"]),
  methods: {
    setAuthInfo(){
        let authTable = [
          {
              "id": 275,       //   菜单ID
              "moduleId": null,
              "moduleCode": "",
              "moduleName": "WIP管理",   //  菜单名称
              "viewList": [
                  {
                      "id": 278,
                      "moduleId": null,
                      "moduleCode": "",
                      "moduleName": "WIP盘点履历",
                      "viewList": [
                          {
                              "id": 279,
                              "moduleId": null,
                              "moduleCode": "",
                              "moduleName": "新建",
                              "viewList": null,
                          },
                          {
                              "id": 280,
                              "moduleId": null,
                              "moduleCode": "",
                              "moduleName": "提交",
                              "viewList": null,
                          }
                      ],
                  }
              ],
            },
        ]
         //获取table中的id字段,放在数组ids中
        let ids = [];
        this.getAuthIds(authTable,ids);
        //把ids数组放在localStorage中
        window.localStorage.setItem('auth',ids);
    },
    getAuthIds(table,ids){
      for(let i = 0 ; i<table.length ; i++){
        ids.push(table[i].id);
        if(table[i].viewList != null && table[i].viewList.length >0){
          this.getAuthIds(table[i].viewList,ids);
        }
      }
    }
    
    }
};
</script>

<style>
html,
body,
#app {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
</style>

指令使用:
通过v-has指令控制按钮显隐。如果v-has指令返回true,则按钮显示,返回false,按钮不显示。

<el-button @click=" dialogVisibleAdd = true" v-has="'279'">新建</el-button>

也可以用这种方式:

<el-button @click=" dialogVisibleAdd = true" v-if="flag ">新建</el-button>
let flag = this.$_has('279');

实现原理后续再补充吧~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值