1、判断字符串是否存在 indexOf
2、对字符串进行分割 split("分隔符")
3、数组获取元素。
pop() 获取最后一个元素
shift() 获取数组第一个元素
slice() 对数组进行切片,获取指定长度的数组,返回一个数组
4、将秒转换成字符串,小时:分钟:秒数格式:
export function getTimes(allSeconds){
let str = '';
let s = allSeconds%60
if(s < 10){
s = '0'+s
}
let m = Math.floor(allSeconds/60)
if(m < 10){
m = '0'+m
}
let h = Math.floor(allSeconds/3600)
if(h < 10){
h = '0'+h
}
if(allSeconds <= 60){
str = allSeconds
}else if(allSeconds > 60 && allSeconds <= 3600){
str = m+':'+s+''
}else{
let other = allSeconds%3600
str = h+':'+ Math.floor(other/60)+':'+ s
}
return str;
}
5、链接地址判断是文件、视频还是图片。
const videoArr = ['mp4', 'mov', 'avi', 'wmv', 'mpeg', 'm4v', 'flv', 'f4v', 'rmvb', 'rm', '3gp', 'vob', 'asf'];
const imageArr = ['jpg', 'png', 'gif', 'jpeg'];
export function checkType(url = '') {
let str = 'file'
if(!url){
return str;
}
let arr = url.split('.')
let ext = arr.pop()
if (videoArr.indexOf(ext) != -1) {
str = 'video'
return str;
}
if (imageArr.indexOf(ext) != -1) {
str = 'image'
return str;
}
return str;
}
6、正则判断手机号格式
export function isvalidPhone(phone) {
const isPhone = /^1[3|4|5|6|7|8|9][0-9]{9}$/
if (isPhone.test(phone)) {
return true
} else {
return false
}
};
在项目开发的过程中,如果一个方法在三个及上个以上的地方用到了,最好的方法是将其进行封装。如果涉及到样式,就封装成组件;如果只涉及到逻辑处理,就封装成方法。