File tree Expand file tree Collapse file tree 4 files changed +54
-0
lines changed Expand file tree Collapse file tree 4 files changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ import type { App } from 'vue'
2
+
3
+ /**
4
+ * 聚焦
5
+ */
6
+ export default function setupFocusDirective ( app : App ) {
7
+ app . directive ( 'focus' , {
8
+ mounted ( el : HTMLElement ) {
9
+ // 聚焦元素
10
+ el . focus ( )
11
+ } ,
12
+ } )
13
+ }
Original file line number Diff line number Diff line change
1
+ import type { App } from 'vue'
2
+
3
+ import setupPermissionDirective from './permission'
4
+ import setupFocusDirective from './focus'
5
+
6
+ /** setup custom vue directives. - [安装自定义的vue指令] */
7
+ export function setupDirectives ( app : App ) {
8
+ setupPermissionDirective ( app )
9
+ setupFocusDirective ( app )
10
+ }
Original file line number Diff line number Diff line change
1
+ /*按钮权限校验*/
2
+ import { App , DirectiveBinding } from 'vue'
3
+
4
+ //权限检查方法
5
+ const hasPermission = function ( value : string ) {
6
+ const permissionList : string [ ] = [ 'edit' , 'delete' ]
7
+ return permissionList . includes ( value )
8
+ }
9
+
10
+ const permissionDirective = function ( el : HTMLElement , binding : DirectiveBinding ) {
11
+ if ( ! binding . value ) {
12
+ throw new Error ( 'v-permission need value' )
13
+ }
14
+ if ( binding . value !== binding . oldValue && ! hasPermission ( binding . value ) ) {
15
+ el ?. parentNode ?. removeChild ( el ) /*删除节点*/
16
+ }
17
+ }
18
+
19
+ /**
20
+ * 权限指令
21
+ */
22
+ export default function setupPermissionDirective ( app : App ) {
23
+ app . directive ( 'permission' , {
24
+ mounted : permissionDirective ,
25
+ updated : permissionDirective ,
26
+ } )
27
+ }
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { createApp } from 'vue'
2
2
import App from './App.vue'
3
3
import { setupStore } from './store'
4
4
import { setupRouter } from './router'
5
+ import { setupDirectives } from './directives'
5
6
import { setupEcharts } from '@/utils/echarts'
6
7
import VueGridLayout from 'vue-grid-layout'
7
8
@@ -31,6 +32,9 @@ async function setupApp() {
31
32
// vue router
32
33
await setupRouter ( app )
33
34
35
+ // vue directives
36
+ setupDirectives ( app )
37
+
34
38
// echarts
35
39
setupEcharts ( app )
36
40
You can’t perform that action at this time.
0 commit comments