1,新建store.js文件

	import Vue from 'vue'
	import Vuex from 'vuex'
	import {
	  GetUserAllAuthorization
	} from '@/api/permissionSetting'
	Vue.use(Vuex)
	const store = new Vuex.Store({
	  state: {
	    roles: [],
	  },
	  getters: {
	    roles: state => state.roles,
	  },
	  mutations: {
		  SET_ROLES: (state, roles) => {
	      state.roles = roles
	    },
	  },
	  actions: {
	  	// 获取用户权限
	    getInfo({
	      commit,
	      state
	    }) {
	      return new Promise((resolve, reject) => {
	        const UserID = sessionStorage.getItem('UserID')
	        GetUserAllAuthorization(UserID).then(response => {
	          const {
	            Content
	          } = response
	          const {
	            Author
	          } = Content
	          const roles = Author.map(el => {
	            return el.Code
	          })
	          // 用户权限代码
	          commit('SET_ROLES', roles)
	          resolve(Content)
	        })
	        // });
	      })
	    },
	  },
	})

2,新建permission.js文件

import store from '@/store';

export default {
  inserted(el, binding, vnode) {
    const { value } = binding;
    const roles = store.getters && store.getters.roles;
    if (value && value instanceof Array && value.length > 0) {
      const permissionRoles = value;
      const hasPermission = roles.some(role => {
        return permissionRoles.includes(role);
      });

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    } else {
      throw new Error(`need roles! Like v-permission="['admin','editor']"`);
    }
  }
};

3,需要用到的a.vue文件里

<script>
  	import {
		import permission from '@/directive/permission/index';
	}
	export default {
	    directives: {
	      permission
	    },
	    name: 'a',
	    data() {
	    }
	}

在html文件中来用即可

<el-select v-permission="['1610']" v-model="TaskUserID" placeholder="全体成员">
Logo

前往低代码交流专区

更多推荐