Element UI 自定义验证
**data写法:**
data() {
const validateNull = (rule, value, callback) => {
if (this.global_trim_all(value) === "") { //global_trim_all 全局验证前后不能有空格
callback(new Error("请输入"));
} else {
callback();
}
};
}
**Element UI 必填验证写法:**
rules: {
result: [{required: true, validator:validateNull, trigger: "blur"}],
},
全局的表单验证方法
import {isNumber} from '../../../utils/validate'
rules: {
{ required: true, trigger: "blur", validator: isNumber } ]
},
watch 监听对象/对象属性
obj:{ //监听的对象
deep:true, //深度监听设置为 true
handler:function(newV,oldV){
console.log('watch中:',newV)
}
}
}
watch:{
'obj.name':{ //监听的对象属性
deep:true,
handler:function(newV,oldV){
console.log('watch中:',newV)
}
}
JS字符串和数组之间的转换
1、字符串转换为数组
var string = '123,456,789';
var stringResult = string.split(',');
console.log(stringResult) //输出["123", "456", "789"]
2、字符串转换为数组
var string2 = 'abcdef'
var string2Result = string2.split('')
console.log(string2Result) //输出['a','b','c','d','e','f']
string2.split(",").map(Number);//输出[123,456,789]
JSON.parse("[" + string + "]"); //输出[123,456,789]
3、数组转换为字符串
var array = ['abc', 'def', 'hig']
var arrayResult = array.join(',')
console.log(arrayResult) // 输出"abc,def,hig"
array.toString()//输出"abc,def,hig"
4、数字数组转化为字符串数组
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.map(String); //结果: ['1', '2', '3', '4', '5', '6', '7', '8', '9']
5、字符串数组转化为数字数组
var a = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
a.map(Number); //结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
props传参obj
planForm:{
type:Object,
default:()=>{
return {}
}
},
判断下拉框是否存在某个值
riskTypeChange() {
this.isShowRisk = this.form.riskType.some((item) => {
return item == this.riskTypeId;
});
if (this.isShowRisk) {
this.$set(this.form, "riskLevel", "");
}
},
遍历数组是否包含某个值
1:array.indexOf 此方法判断数组中是否存在某个值,如果存在返回数组元素的下标,否则返回-1。
let arr = ['something', 'anything', 'nothing', 'anything'];
let index = arr.indexOf('nothing');
console.log(index) //结果是2
2、array.includes(searchElement[, fromIndex]) 此方法判断数组中是否存在某个值,如果存在返回 true,否则返回false。
function test(fruit) {
const redFruits = ['apple', 'strawberry', 'cherry', 'cranberries'];
if (redFruits.includes(fruit)) {
console.log('red');
}else{
console.log('blue');
}
}
3、array.findIndex(callback[, thisArg]) 返回数组中满足条件的第一个元素的索引(下标), 如果没有找到,返回-1 同第4种方法类似;
4、array.find(callback[, thisArg]) 返回数组中满足条件的第一个元素的值,如果没有,返回undefined
// ---------- 元素是普通字面值 ----------
let numbers = [12, 5, 8, 130, 44];
let result = numbers.find(item => {
return item > 8;
});
console.log(result)
# 结果: 12
// ---------- 元素是对象 ----------
let items = [
{id: 1, name: 'something'},
{id: 2, name: 'anything'},
{id: 3, name: 'nothing'},
{id: 4, name: 'anything'}
];
let item = items.find(item => {
return item.id == 3;
});