在项目的 src\directive\index.js 建立根文件, 方便main.js内批量引入
import clc from './clickConfirm'
const directives = {
clc
}
export default {
install(Vue) {
Object.keys(directives).forEach((key) => {
Vue.directive(key, directives[key])
})
}
}
相同目录下 , 建立自定义指令文件
import { MessageBox, Message } from 'element-ui'
const clickConfirm = {
bind(el, binding, vnode) {
const { value, arg, modifiers } = binding
el.$arg = arg
if (el.$arg === 'AM_1000' || el.$arg === 'AM_1003' || el.$arg === '') {
if (el.$arg === 'AM_1000' || el.$arg === '') {
el.addEventListener('click', tipsUn)
}
if (el.$arg === 'AM_1003') {
el.addEventListener('click', tipsDown)
}
} else {
el.addEventListener('click', value)
}
const newModifiers = Object.keys(modifiers)
if (newModifiers.length > 0) {
el.addEventListener('click', (event) => {
newModifiers.forEach((e) => {
switch (e) {
case 'stop':
event.stopPropagation()
break
case 'prevent':
event.preventDefault()
break
default:
break
}
})
})
}
function tipsUn() {
MessageBox.confirm('当前平台暂未授权,请激活授权后重试!', {
cancelButtonText: '我知道了',
showConfirmButton: false,
type: 'warning',
cancelButtonClass: 'btn-custom-cancel',
customClass: 'safetyAnalysis_messageBox'
}).catch((err) => {
console.log(err)
})
}
function tipsDown() {
MessageBox.confirm('授权已到期,请更新授权后重试!', {
cancelButtonText: '我知道了',
showConfirmButton: false,
type: 'warning',
cancelButtonClass: 'btn-custom-cancel',
customClass: 'safetyAnalysis_messageBox'
}).catch((err) => {
console.log(err)
})
}
},
componentUpdated(el, binding) {
el.$arg = binding.arg
},
unbind(el, binding) {
const { value, arg, modifiers } = binding
el.removeEventListener('click', value)
}
}
export default clickConfirm
使用方法 , 不能使用内联式js
//正确写法
<div
v-clc:[authorization].prevent="emitNewFormVisibleShow"
class="hover_btn"
>
<i class="iconfont iconnormal1" />
<span class="txt">新增</span>
</div>
//错误写法
<div
v-clc:[authorization].prevent="$emit('test')"
class="hover_btn"
>
<i class="iconfont iconnormal1" />
<span class="txt">新增</span>
</div>
//authorization为全局动态赋值 //prevent为自定义指令的修饰符,可按要求扩展
|