Skip to content

Commit

Permalink
按钮自定义指令
Browse files Browse the repository at this point in the history
  • Loading branch information
bigCows committed Sep 21, 2023
1 parent 7dedadf commit 73fccd5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
8 changes: 3 additions & 5 deletions src/demo/diretive-demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<input type="text" v-focus="'120'" />
<p v-height="'100'"></p>
<p v-myText:bar.stop="obj">p标签</p>

<el-button v-auth="'btnAuth1'">按钮权限</el-button>
<el-button v-auth="['edit','btnAuth1']">按钮权限arr/err</el-button>
<el-button v-auth="['btnAuth1','btnAuth2']">按钮权限arr/success</el-button>
</div>
</template>

Expand All @@ -18,14 +20,10 @@ const obj = ref({
// 自定义指令
const vFocus = {
mounted(el: HTMLElement, binding: any) {
console.log(el,'el');
console.log(binding,'binding');
el.focus()
el.classList.add('my-height')
el.style.color = 'red'
// console.log(el.attributes
// ,'el.attributes');
el.setAttribute('value','我是value')
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/directive/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type {App, Directive} from 'vue'
import myText from "./modules/text";
import height from "./modules/height";

import auth from './modules/auth';

const directiveList: {[key:string] : Directive} = {
myText,
height
height,
auth
}

const directives = {
Expand Down
15 changes: 15 additions & 0 deletions src/directive/modules/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Directive,DirectiveBinding } from 'vue'

const btnAuthList = ['btnAuth1', 'btnAuth2', 'btnAuth3']
const auth: Directive = {
mounted(el:HTMLElement, binding:DirectiveBinding) {
const { value } = binding;
if(Array.isArray(value) && value.length) {
value.forEach(item => btnAuthList.includes(item) || el.remove())
} else {
btnAuthList.includes(value) || el.remove();
}
}
}

export default auth;
17 changes: 7 additions & 10 deletions src/directive/modules/height.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { Directive,DirectiveBinding } from 'vue'

import type { Directive,DirectiveBinding } from 'vue'
const height:Directive = {
mounted(el:HTMLElement, binding:DirectiveBinding, vnode, preVnode) {
console.log('123');
console.log(binding, 'binding11111111');

el.style.height = binding.value + 'px'
el.style.width = binding.value + 'px'
el.innerText = binding.value + 'px'
el.style.border = 'dashed red'
mounted(el:HTMLElement, binding:DirectiveBinding, vnode, preVnode) {
el.style.width = binding.value + 'px'
el.innerText = binding.value + 'px'
el.style.height = binding.value + 'px'
el.style.border = 'dashed red'
}
}

export default height;
export default height;
10 changes: 3 additions & 7 deletions src/directive/modules/text.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import type { Directive,DirectiveBinding } from 'vue'

const myText:Directive = {
mounted(el:HTMLElement, binding:DirectiveBinding, vnode, preVnode) {
console.log(binding, 'binding');
// console.log(vnode, 'vnode');
// console.log(preVnode, 'preVnode');

mounted(el:HTMLElement, binding:DirectiveBinding) {
// el.style.height = binding.value + 'px'
// el.innerText = binding.value + 'px'
el.innerText = binding.value ? binding.value.name.split('').reverse().join('') : '无法识别';
}
el.innerText = binding.value ? binding.value.name.split('').reverse().join('') : '无法识别';
}
}

export default myText;
1 change: 0 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import directives from '@/directive/index'


const app = createApp(App)

// 注册全局指令
app.use(directives)

Expand Down

0 comments on commit 73fccd5

Please sign in to comment.