项目中当使用了输入框时,但是输入框只能限定使用数字,所以这个时候就需要对输入框输入的内容进行限定处理,但是每次都使用方法进行限定,势必是不科学的,如果直接通过指令限定是不是会更加的科学,下面结合网络大神的代码,加上自己的一些小小的修改,实现自定义指令的封装,代码如下
1、需要在main.js下引用指令所在的文件
// 引用自定义指令文件,文件放在src下的directive目录下的el-input目录下
import onlyNumber from '@/directive/el-input'
// 声明全局使用自定义指令
Vue.use(onlyNumber)
2、src/directive/el-input/index.js
import onlyNumber from './onlyNumber'
const install = Vue => {
Vue.directive('onlyNumber', onlyNumber)
}
/*
Vue.use( plugin )
安装 Vue.js 插件。如果插件是一个对象,必须提供 install 方法。
如果插件是一个函数,它会被作为 install 方法。install 方法调用时,会将 Vue 作为参数传入。
该方法需要在调用 new Vue() 之前被调用。
当 install 方法被同一个插件多次调用,插件将只会被安装一次。
*/
if (window.Vue) {
window['onlyNumber'] = onlyNumber
Vue.use(install); // eslint-disable-line
}
onlyNumber.install = install
export default onlyNumber
3、src/derective/el-input/onlyNumber.vue
export default {
inserted(el, vDir, vNode) {
// vDir.value 有指令的参数
let content
// 设置输入框的值,触发input事件,改变v-model绑定的值
const setVal = val => {
if (vNode.componentInstance) {
// 如果是自定义组件就触发自定义组件的input事件
vNode.componentInstance.$emit('input', val)
} else {
// 如果是原生组件就触发原生组件的input事件
el.value = val
el.dispatchEvent(new Event('input'))
}
}
// 按键按下=>只允许输入 数字/小数点/减号
el.addEventListener('keypress', event => {
const e = event || window.event
const inputKey = String.fromCharCode(typeof e.charCode === 'number' ? e.charCode : e.keyCode)
const re = /\d|\.|-/
content = e.target.value
// 定义方法,阻止输入
function preventInput() {
if (e.preventDefault) {
e.preventDefault()
} else {
e.returnValue = false
}
}
if (!re.test(inputKey) && !e.ctrlKey) {
preventInput()
} else if (content.indexOf('.') > 0 && inputKey === '.') {
// 已有小数点,再次输入小数点
preventInput()
}
})
// 按键弹起=>并限制最大最小
// el.addEventListener('keyup', event => {
// const e = event || window.event
// content = parseFloat(e.target.value)
// if (!content) {
// content = 0.00
// }
// let arg_max = ''
// let arg_min = ''
// if (vDir.value) {
// arg_max = parseFloat(vDir.value.max)
// arg_min = parseFloat(vDir.value.min)
// }
// if (arg_max !== undefined && content > arg_max) {
// setVal(arg_max)
// content = arg_max
// }
// if (arg_min !== undefined && content < arg_min) {
// setVal(arg_min)
// content = arg_min
// }
// })
// 失去焦点=>保留指定位小数
el.addEventListener('focusout', event => { // 此处会在 el-input 的 @change 后执行
const e = event || window.event
content = parseFloat(e.target.value)
if (!content) {
// content = 0.00
if (content == 0) {
content = 0.00
} else {
content = ''
}
}
let arg_precision = 0// 默认保留至整数
if (vDir.value.precision) {
arg_precision = parseFloat(vDir.value.precision)
}
// e.target.value = content.toFixed(arg_precision)
e.target.value = content ? content.toFixed(arg_precision) : vDir.value.min
let arg_max = ''
let arg_min = ''
if (vDir.value) {
arg_max = parseFloat(vDir.value.max) + '' // 这里特意转化成字符串形式 防止当最大或者最小只为0时,判断为false
arg_min = parseFloat(vDir.value.min) + '' // 这里特意转化成字符串形式 防止当最大或者最小只为0时,判断为false
}
if (arg_max && +content > arg_max) {
e.target.value = arg_max
content = arg_max
}
if (arg_min && +content < arg_min) {
e.target.value = arg_min
content = arg_min
}
setVal(e.target.value)
// -- callback写法1
// vNode.data.model.callback = ()=>{
// e.target.value = content.toFixed(arg_precision)
// }
// vNode.data.model.callback();
// -- callback 写法2
// vNode.data.model.callback(
// e.target.value = content.toFixed(arg_precision)
// )
})
}
}
4、在页面中使用
// max 为限定的最大值;min 为限定的最小值;precision 为保留的小时位数
<el-input
v-model="formProp[item.formProp][item.prop]"
v-only-number="{max: 100, min: 0,precision:0}"
:placeholder="$t('steamship.' + item.placeholder)"
@change="changeInputFn"
/>
如果直接使用的话,可以根据自己的需求适当的修改优化。
如有不对之处请指正,互相学习!