公共js:utils.js
导出方法:module.exports={公共方法名:定义的方法名}
const formatTime = 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
}
module.exports = {
formatTime: formatTime
}
公共方法引用:
在需要引用的js里进行引用
const formatTime=require('../../utils/util.js');//进行引用
console.log(formatTime(new Date()));//调用