File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ import store from '@/store'
2
+
3
+ /**
4
+ * @param {Array } value
5
+ * @returns {Boolean }
6
+ * @example see @/views/permission/directive.vue
7
+ */
8
+ export default function checkPermission ( value ) {
9
+ if ( value && value instanceof Array && value . length > 0 ) {
10
+ const roles = store . getters && store . getters . roles
11
+ const permissionRoles = value
12
+
13
+ const hasPermission = roles . some ( role => {
14
+ return permissionRoles . includes ( role )
15
+ } )
16
+
17
+ if ( ! hasPermission ) {
18
+ return false
19
+ }
20
+ return true
21
+ } else {
22
+ console . error ( `need roles! Like v-permission="['admin','editor']"` )
23
+ return false
24
+ }
25
+ }
26
+
Original file line number Diff line number Diff line change 16
16
<el-tag class =" permission-tag" size =" small" >editor</el-tag > can see this
17
17
</span >
18
18
</div >
19
+
20
+ <div style =" margin-top :30px ;" :key =" 'checkPermission'+key" >
21
+ <code >In some cases it is not suitable to use v-permission, such as element Tab component which can only be achieved by manually setting the v-if.
22
+ <br > e.g.
23
+ </code >
24
+ <el-tabs type =" border-card" style =" width :500px ;" >
25
+ <el-tab-pane v-if =" checkPermission(['admin'])" label =" Admin" >Admin can see this</el-tab-pane >
26
+ <el-tab-pane v-if =" checkPermission(['editor'])" label =" Editor" >Editor can see this</el-tab-pane >
27
+ <el-tab-pane v-if =" checkPermission(['admin','editor'])" label =" Admin-OR-Editor" >Both admin or editor can see this</el-tab-pane >
28
+ </el-tabs >
29
+ </div >
19
30
</div >
20
31
</template >
21
32
22
33
<script >
23
34
import permission from ' @/directive/permission/index.js' // 权限判断指令
35
+ import checkPermission from ' @/utils/permission' // 权限判断函数
24
36
import SwitchRoles from ' ./components/SwitchRoles'
25
37
26
38
export default {
@@ -33,6 +45,7 @@ export default{
33
45
}
34
46
},
35
47
methods: {
48
+ checkPermission,
36
49
handleRolesChange () {
37
50
this .key ++
38
51
}
You can’t perform that action at this time.
0 commit comments