diff --git a/src/directive/permission/index.js b/src/directive/permission/index.js new file mode 100644 index 00000000..e5dadd36 --- /dev/null +++ b/src/directive/permission/index.js @@ -0,0 +1,13 @@ +import permission from './permission' + +const install = function(Vue) { + Vue.directive('permission', permission) +} + +if (window.Vue) { + window['permission'] = permission + Vue.use(install); // eslint-disable-line +} + +permission.install = install +export default permission diff --git a/src/directive/permission/permission.js b/src/directive/permission/permission.js new file mode 100644 index 00000000..17b85d79 --- /dev/null +++ b/src/directive/permission/permission.js @@ -0,0 +1,23 @@ + +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']"`) + } + } +} diff --git a/src/lang/en.js b/src/lang/en.js index fc0405c9..22615692 100644 --- a/src/lang/en.js +++ b/src/lang/en.js @@ -4,6 +4,8 @@ export default { introduction: 'Introduction', documentation: 'Documentation', permission: 'Permission', + pagePermission: 'Page Permission', + directivePermission: 'Directive Permission', icons: 'Icons', components: 'Components', componentIndex: 'Introduction', diff --git a/src/lang/zh.js b/src/lang/zh.js index 41c959c3..c7df04ed 100644 --- a/src/lang/zh.js +++ b/src/lang/zh.js @@ -4,6 +4,8 @@ export default { introduction: '简述', documentation: '文档', permission: '权限测试页', + pagePermission: '页面权限', + directivePermission: '指令权限', icons: '图标', components: '组件', componentIndex: '介绍', diff --git a/src/router/index.js b/src/router/index.js index 70e51d36..009963a3 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -67,16 +67,28 @@ export const asyncRouterMap = [ path: '/permission', component: Layout, redirect: '/permission/index', - meta: { roles: ['admin'] }, // you can set roles in root nav + alwaysShow: true, // will always show the root menu + meta: { + title: 'permission', + icon: 'lock', + roles: ['admin', 'editor'] // you can set roles in root nav + }, children: [{ - path: 'index', - component: _import('permission/index'), - name: 'permission', + path: 'page', + component: _import('permission/page'), + name: 'pagePermission', meta: { - title: 'permission', - icon: 'lock', + title: 'pagePermission', roles: ['admin'] // or you can only set roles in sub nav } + }, { + path: 'directive', + component: _import('permission/directive'), + name: 'directivePermission', + meta: { + title: 'directivePermission' + // if do not set roles, means: this page does not require permission + } }] }, diff --git a/src/views/permission/components/SwitchRoles.vue b/src/views/permission/components/SwitchRoles.vue new file mode 100644 index 00000000..e898939b --- /dev/null +++ b/src/views/permission/components/SwitchRoles.vue @@ -0,0 +1,30 @@ + + + {{$t('permission.roles')}}: {{roles}} + {{$t('permission.switchRoles')}}: + + + + + + + + diff --git a/src/views/permission/directive.vue b/src/views/permission/directive.vue new file mode 100644 index 00000000..c7028940 --- /dev/null +++ b/src/views/permission/directive.vue @@ -0,0 +1,56 @@ + + + + + + + Only admin can see this + + + Only editor can see this + + + Both admin and editor can see this + + + + + + + + + diff --git a/src/views/permission/index.vue b/src/views/permission/index.vue deleted file mode 100644 index 9056a076..00000000 --- a/src/views/permission/index.vue +++ /dev/null @@ -1,34 +0,0 @@ - - - {{$t('permission.roles')}}: {{roles}} - {{$t('permission.switchRoles')}}: - - - - - - - diff --git a/src/views/permission/page.vue b/src/views/permission/page.vue new file mode 100644 index 00000000..b36ee083 --- /dev/null +++ b/src/views/permission/page.vue @@ -0,0 +1,19 @@ + + + + + + +