1、判断值是否为空
export function valIsNull (val) {
if (val === '' || val === null || val === undefined || val === 'null' || val === 'undefined' || JSON.stringify(val) == '{}' || JSON.stringify(val) == '[]') {
return true
} else {
return false
}
}
2、防抖函数
/**
* 函数防抖,默认最后一次点击后0.5秒触发
* @param fn
* @param delay
* @returns {Function}
* @constructor
*/
export const _debounce = (fn, t) => {
const delay = t || 500
let timer
return function () {
const args = arguments
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
timer = null
fn.apply(this, args)
}, delay)
}
}
3、节流函数
/**
* 函数节流,默认间隔1秒触发
* @param fn
* @param interval
* @returns {Function}
* @constructor
*/
export const _throttle = (fn, t) => {
let last
let timer
const interval = t || 1000
return function () {
const args = arguments
const now = +new Date()
if (last && now - last < interval) {
clearTimeout(timer)
timer = setTimeout(() => {
last = now
}, interval)
} else {
last = now
fn.apply(this, args)
}
}
}
4、深度克隆
export function deepClone (target, cache = new WeakMap()) {
if (target === null || typeof target !== 'object') {
return target
}
if (cache.get(target)) {
return target
}
const copy = Array.isArray(target) ? [] : {}
cache.set(target, copy)
Object.keys(target).forEach(function (key) {
copy[key] = deepClone(target[key], cache)
})
return copy
}
5、生成一个随机uuid
/**
* 生成一个随机uuid
*/
export function guid () {
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0; var v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
6、eventBus
bus.js:
import Vue from 'vue';
export default new Vue;
main.js:
import bus from '@/utils/bus.js';
Vue.prototype.$Bus = bus;
使用:
this.$Bus.$emit('payMent',{id,payment: 'WxPay',checkYe: false})
this.$Bus.$on('payMent', async (item) => {})
7、moment插件
package.json
"dependencies": {
"moment": "^2.24.0"
},
main.js
import moment from 'moment'
moment.locale('zh-cn')
Vue.prototype.$moment = moment
使用:
$moment(time * 1000).format('YYYY-MM-DD')
$moment(time * 1000).format('YYYY-MM-DD HH:mm:ss')