admin 管理员组

文章数量: 887031

v

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} = responseconst {Author} = Contentconst 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="全体成员">

本文标签: v