vue封装的一些函数

↘锁芯ラ 提交于 2020-01-10 20:49:44

因项目要用到一些常用的函数,故简单的封装了些函数,方便自己以后调用把

utils.js:

//根据数组中字段排序,从小到大

export const compare = (property) => {

  return function (a, b) {

    var value1 = a[property];

    var value2 = b[property];

    return value1 - value2;

  }

}

//根据数组中字段排序,从大到小

export const compare_big = (property) => {

  return function (a, b) {

    var value1 = a[property];

    var value2 = b[property];

    return value2 - value1;

  }

}
/**
 * 获取当前时间
 * 格式YYYY-MM-DD
 */
export const getDate = () => {
	var date = new Date();
	var seperator1 = "-";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}
/**
 * 获取当前时间,为订单号提供
 * 格式YYYYMMDDHHMMSS
 */
export const getDateNums = (date) => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('') + [hour, minute, second].map(formatNumber).join('')
}
const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

调用方式,这里只是举个例子:

import { getDateNums } from "@/common/utils.js";

        order_nums() {
           //调用
            var outTradeNo = String(getDateNums(new Date()))
        },

比较大小

//gg_type为你需要排序的数组,“gg_id”排序的字段
gg_type.sort(compare("gg_id"));
this.gg_type = gg_type;

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!