Skip to content

Commit 305751f

Browse files
committed
指令配置
1 parent a656fbf commit 305751f

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/directives/focus.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

src/directives/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

src/directives/permission.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createApp } from 'vue'
22
import App from './App.vue'
33
import { setupStore } from './store'
44
import { setupRouter } from './router'
5+
import { setupDirectives } from './directives'
56
import { setupEcharts } from '@/utils/echarts'
67
import VueGridLayout from 'vue-grid-layout'
78

@@ -31,6 +32,9 @@ async function setupApp() {
3132
// vue router
3233
await setupRouter(app)
3334

35+
// vue directives
36+
setupDirectives(app)
37+
3438
// echarts
3539
setupEcharts(app)
3640

0 commit comments

Comments
 (0)