首页 > web前端 > js教程 > 正文

关于vue项目工具文件utils.js使用的代码分享

零到壹度
发布: 2018-04-13 16:51:02
原创
3491人浏览过

本篇文章给大家分享的内容是关于vue项目工具文件utils.js使用的代码分享,有着一定的参考价值,有需要的朋友可以参考一下

import Vue from 'vue'
import XLSX from 'xlsx'
/**
 * 图片预览方法
 * 已注入所有Vue实例,
 * template模板里调用 $imgPreview(src)
 * 组件方法里调用 this.$imgPreview(src)
 */
const imgPreview = (src) => {
let p = document.createElement('p')
let img = document.createElement('img')
let close = document.createElement('i')
p.className = 'body__img__preview'
img.src = src
close.className = 'body__img__close'
close.onclick = () => {
document.body.removeChild(p)
}
p.appendChild(img)
p.appendChild(close)
document.body.appendChild(p)
}
/**
 * 格式化日期方法
 * 已注入所有Vue实例,
 * template模板里调用 $dateFormat(date)
 * 组件方法里调用 this.$dateFormat(date)
 * 例:this.$dateFormat('Dec 27, 2017 3:18:14 PM') 得到 '2017-12-27 15:18:14'
 */
const dateFormat = (date) => {
if (!date) return ''
let date_format = new Date(date)
let year = date_format.getFullYear()
let month = date_format.getMonth() + 1
if (month < 10) month = '0' + month
let mydate = date_format.getDate()
if (mydate < 10) mydate = '0' + mydate
let hours = date_format.getHours()
if (hours < 10) hours = '0' + hours
let minutes = date_format.getMinutes()
if (minutes < 10) minutes = '0' + minutes
let seconds = date_format.getSeconds()
if (seconds < 10) seconds = '0' + seconds
let time = `${year}-${month}-${mydate} ${hours}:${minutes}:${seconds}`
return time
}
/**
 * 格式化日期方法
 * 已注入所有Vue实例,
 * template模板里调用 $dateFormatNoTime(date)
 * 组件方法里调用 this.$dateFormatNoTime(date)
 * 例:this.$dateFormatNoTime('Dec 27, 2017 3:18:14 PM') 得到 '2017-12-27'
 */
const dateFormatNoTime = (date) => {
if (!date) return ''
let date_format = new Date(date)
let year = date_format.getFullYear()
let month = date_format.getMonth() + 1
if (month < 10) month = '0' + month
let mydate = date_format.getDate()
if (mydate < 10) mydate = '0' + mydate
let time = `${year}-${month}-${mydate}`
return time
}
/**
 * 获取当天日期方法
 * 已注入所有Vue实例,
 * template模板里调用 $todayFormat
 * 组件方法里调用 this.$todayFormat
 * 例:this.$todayFormat() 得到 '2018-01-31'
 */
const todayFormat = () => {
let date_format = new Date()
let year = date_format.getFullYear()
let month = date_format.getMonth() + 1
if (month < 10) month = '0' + month
let date = date_format.getDate()
if (date < 10) date = '0' + date
let time = `${year}-${month}-${date}`
return time
}
/**
 * 导出数据报表xlsx文件
 * 已注入所有Vue实例,
 * template模板里调用 $$outputXlsxFile
 * 组件方法里调用 this.$outputXlsxFile
 * 例:this.$outputXlsxFile([['字段1', '字段2'], [1, 2]], [{wch: 10}, {wch: 50}], '测试导出') 得到 测试导出.xlsx 文件
 * 第一个参数是导出的数组对象,第二个参数是设置字段宽度,第三个参数是文件名
 */
const outputXlsxFile = (data, wscols, xlsxName) => {
/* convert state to workbook */
const ws = XLSX.utils.aoa_to_sheet(data)
ws['!cols'] = wscols
const wb = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(wb, ws, xlsxName)
/* generate file and send to client */
XLSX.writeFile(wb, xlsxName + ".xlsx")
}
/**
 * 判断开始时间和结束时间
 * 已注入所有Vue实例,
 * template模板里调用 $checkTime
 * 组件方法里调用 this.$checkTime
 * 例:this.$checkTime(['2018-01-01', '2018-02-02']) 得到 {'2018/01/01', '2018/02/02'}
 */
const checkTime = (date) => {
if (!date) {
Vue.prototype.$notify.error({
message: '日期不能为空'
})
return false
} else {
let begTime = date[0].replace(/-/g, '/')
let endTime = date[1].replace(/-/g, '/')
if (+((new Date(endTime)).getTime()) < +((new Date(begTime)).getTime())) {
Vue.prototype.$notify.error({
message: '开始日期不能大于结束日期'
})
return false
} else {
begTime = date[0]
endTime = date[1]
return {begTime, endTime}
}
}
}
/**
 * 判断性别
 * 已注入所有Vue实例,
 * template模板里调用 $typeSex
 * 组件方法里调用 this.$typeSex
 * 例:this.$typeSex(1) 得到 男
 * 参数 0:未知 1:男 2:女
 */
const typeSex = (value) => {
let sex = ''
switch (value) {
case '0' : sex = '未知'
break
case '1' : sex = '男'
break
case '2' : sex = '女'
break
}
return sex
}
/**
 * 时间戳转换
 * 已注入所有Vue实例,
 * template模板里调用 $timestampToTime
 * 组件方法里调用 this.$timestampToTime
 * 例:this.$timestampToTime(1523440865000) 得到 '2018-04-11 18:1:5'
 */
const timestampToTime = (timestamp) => {
var date = new Date(timestamp)
let Y = date.getFullYear() + '-'
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
let D = date.getDate() + ' '
let h = date.getHours() + ':'
let m = date.getMinutes() + ':'
let s = date.getSeconds()
return Y + M + D + h + m + s
}
Vue.prototype.$imgPreview = imgPreview
Vue.prototype.$dateFormat = dateFormat
Vue.prototype.$dateFormatNoTime = dateFormatNoTime
Vue.prototype.$todayFormat = todayFormat
Vue.prototype.$outputXlsxFile = outputXlsxFile
Vue.prototype.$checkTime = checkTime
Vue.prototype.$typeSex = typeSex
Vue.prototype.$timestampToTime = timestampToTime
登录后复制


相关推荐:

vue使用xe-utils详细教程

代码小浣熊
代码小浣熊

代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

代码小浣熊 51
查看详情 代码小浣熊

以上就是关于vue项目工具文件utils.js使用的代码分享的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号