常用utils

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')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值