File tree Expand file tree Collapse file tree 9 files changed +163
-40
lines changed Expand file tree Collapse file tree 9 files changed +163
-40
lines changed Original file line number Diff line number Diff line change
1
+ import permission from './permission'
2
+
3
+ const install = function ( Vue ) {
4
+ Vue . directive ( 'permission' , permission )
5
+ }
6
+
7
+ if ( window . Vue ) {
8
+ window [ 'permission' ] = permission
9
+ Vue . use ( install ) ; // eslint-disable-line
10
+ }
11
+
12
+ permission . install = install
13
+ export default permission
Original file line number Diff line number Diff line change
1
+
2
+ import store from '@/store'
3
+
4
+ export default {
5
+ inserted ( el , binding , vnode ) {
6
+ const { value } = binding
7
+ const roles = store . getters && store . getters . roles
8
+
9
+ if ( value && value instanceof Array && value . length > 0 ) {
10
+ const permissionRoles = value
11
+
12
+ const hasPermission = roles . some ( role => {
13
+ return permissionRoles . includes ( role )
14
+ } )
15
+
16
+ if ( ! hasPermission ) {
17
+ el . parentNode && el . parentNode . removeChild ( el )
18
+ }
19
+ } else {
20
+ throw new Error ( `need roles! Like v-permission="['admin','editor']"` )
21
+ }
22
+ }
23
+ }
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ export default {
4
4
introduction : 'Introduction' ,
5
5
documentation : 'Documentation' ,
6
6
permission : 'Permission' ,
7
+ pagePermission : 'Page Permission' ,
8
+ directivePermission : 'Directive Permission' ,
7
9
icons : 'Icons' ,
8
10
components : 'Components' ,
9
11
componentIndex : 'Introduction' ,
Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ export default {
4
4
introduction : '简述' ,
5
5
documentation : '文档' ,
6
6
permission : '权限测试页' ,
7
+ pagePermission : '页面权限' ,
8
+ directivePermission : '指令权限' ,
7
9
icons : '图标' ,
8
10
components : '组件' ,
9
11
componentIndex : '介绍' ,
Original file line number Diff line number Diff line change @@ -67,16 +67,28 @@ export const asyncRouterMap = [
67
67
path : '/permission' ,
68
68
component : Layout ,
69
69
redirect : '/permission/index' ,
70
- meta : { roles : [ 'admin' ] } , // you can set roles in root nav
70
+ alwaysShow : true , // will always show the root menu
71
+ meta : {
72
+ title : 'permission' ,
73
+ icon : 'lock' ,
74
+ roles : [ 'admin' , 'editor' ] // you can set roles in root nav
75
+ } ,
71
76
children : [ {
72
- path : 'index ' ,
73
- component : _import ( 'permission/index ' ) ,
74
- name : 'permission ' ,
77
+ path : 'page ' ,
78
+ component : _import ( 'permission/page ' ) ,
79
+ name : 'pagePermission ' ,
75
80
meta : {
76
- title : 'permission' ,
77
- icon : 'lock' ,
81
+ title : 'pagePermission' ,
78
82
roles : [ 'admin' ] // or you can only set roles in sub nav
79
83
}
84
+ } , {
85
+ path : 'directive' ,
86
+ component : _import ( 'permission/directive' ) ,
87
+ name : 'directivePermission' ,
88
+ meta : {
89
+ title : 'directivePermission'
90
+ // if do not set roles, means: this page does not require permission
91
+ }
80
92
} ]
81
93
} ,
82
94
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <div >
3
+ <div style =" margin-bottom :15px ;" >{{$t('permission.roles')}}: {{roles}}</div >
4
+ {{$t('permission.switchRoles')}}:
5
+ <el-radio-group v-model =" switchRoles" >
6
+ <el-radio-button label =" editor" ></el-radio-button >
7
+ <el-radio-button label =" admin" ></el-radio-button >
8
+ </el-radio-group >
9
+ </div >
10
+ </template >
11
+
12
+ <script >
13
+ export default {
14
+ computed: {
15
+ roles () {
16
+ return this .$store .getters .roles
17
+ },
18
+ switchRoles: {
19
+ get () {
20
+ return this .roles [0 ]
21
+ },
22
+ set (val ) {
23
+ this .$store .dispatch (' ChangeRoles' , val).then (() => {
24
+ this .$emit (' change' )
25
+ })
26
+ }
27
+ }
28
+ }
29
+ }
30
+ </script >
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <div class =" app-container" >
3
+ <switch-roles @change =" handleRolesChange" />
4
+
5
+ <div :key =" key" style =" margin-top :30px ;" >
6
+ <span v-permission =" ['admin']" class =" permission-alert" >
7
+ Only <el-tag class =" permission-tag" size =" small" >admin</el-tag > can see this
8
+ </span >
9
+ <span v-permission =" ['editor']" class =" permission-alert" >
10
+ Only <el-tag class =" permission-tag" size =" small" >editor</el-tag > can see this
11
+ </span >
12
+ <span v-permission =" []" class =" permission-alert" >
13
+ Both <el-tag class =" permission-tag" size =" small" >admin</el-tag > and <el-tag class =" permission-tag" size =" small" >editor</el-tag > can see this
14
+ </span >
15
+ </div >
16
+ </div >
17
+ </template >
18
+
19
+ <script >
20
+ import permission from ' @/directive/permission/index.js' // 权限判断指令
21
+ import SwitchRoles from ' ./components/SwitchRoles'
22
+
23
+ export default {
24
+ name: ' directivePermission' ,
25
+ components: { SwitchRoles },
26
+ directives: { permission },
27
+ data () {
28
+ return {
29
+ key: 1 // 为了能每次切换权限的时候重新初始化指令
30
+ }
31
+ },
32
+ methods: {
33
+ handleRolesChange () {
34
+ this .key ++
35
+ }
36
+ }
37
+ }
38
+ </script >
39
+
40
+ <style rel="stylesheet/scss" lang="scss" scoped>
41
+ .app-container {
42
+ /deep / .permission-alert {
43
+ width : 320px ;
44
+ margin-top : 30px ;
45
+ background-color : #f0f9eb ;
46
+ color : #67c23a ;
47
+ padding : 8px 16px ;
48
+ border-radius : 4px ;
49
+ display : block ;
50
+ }
51
+ /deep / .permission-tag {
52
+ background-color : #ecf5ff ;
53
+ }
54
+ }
55
+ </style >
56
+
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ <template >
2
+ <div class =" app-container" >
3
+ <switch-roles @change =" handleRolesChange" />
4
+ </div >
5
+ </template >
6
+
7
+ <script >
8
+ import SwitchRoles from ' ./components/SwitchRoles'
9
+
10
+ export default {
11
+ name: ' pagePermission' ,
12
+ components: { SwitchRoles },
13
+ methods: {
14
+ handleRolesChange () {
15
+ this .$router .push ({ path: ' /permission/index?' + + new Date () })
16
+ }
17
+ }
18
+ }
19
+ </script >
You can’t perform that action at this time.
0 commit comments