16、将文案复制到黏贴
copyText(text) {
const input = document.createElement('input')
input.style.position = 'absolute'
input.style.zIndex = -10
document.body.appendChild(input)
input.setAttribute('value', text)
input.select()
document.execCommand('copy')
document.body.removeChild(input)
},
17、downloadFile() post方式导出excel
/**
* @method downloadFile() post方式导出excel
* @param {String} url 接口
* @param {Json} data 数据
*/
downloadFile(url, data) {
const body = document.getElementsByTagName('body')[0]
const form = document.createElement('form')
form.method = 'POST'
form.action = window.location.origin + '/' + url
form.style.display = 'none'
for (var key in data) {
var param = document.createElement('input')
param.type = "hidden"
param.name = key
param.value = data[key]
form.appendChild(param)
}
body.appendChild(form)
form.submit()
body.removeChild(form)
},
downLoad(url, data, fileName) {
var param = ''
for (var key in data) {
param += key + "=" + data[key] + "&"
}
param = param.slice(0, param.length - 1)
window.location.href = url + "?" + param + "&token=" + localStorage.getItem('token') + "&userId=" + localStorage.getItem('userId') + "&userName=" + localStorage.getItem('userName')
},
18、加载script
loadCss (url) {
return new Promise((resolve, reject) => {
let fontLink = document.createElement('link')
fontLink.setAttribute('rel', 'stylesheet')
fontLink.setAttribute('href', url)
document.head.appendChild(fontLink)
fontLink.onload = function () {
resolve(url)
}
})
},
19、计算flex布局需补白个数
countLen(boxDom, pW, liW, data) {
let arr = []
data.forEach((item) => {
if (JSON.stringify(item) !== '{}') arr.push(item)
})
let w = boxDom.offsetWidth - pW
let g = parseInt(w / liW, 10)
let len = arr.length
if (len > 0) {
for (var i = 0; i < (g - len % g); i++) {
arr.push({})
}
}
return arr
},
20、获取某个月份的天数
getMonthDays(month, year) {
let [m, y] = [month, year]
let date = new Date()
if (!month) m = date.getMonth() + 1
if (!year) y = date.getFullYear()
m = parseInt(m, 10)
let d = new Date(m, y, 0)
return d.getDate()
},
21、map
strMapToObj(strMap) {
let obj = {}
for (let [k, v] of strMap) {
obj[k] = v
}
return obj
},
objToStrMap(obj) {
let strMap = new Map()
for (let k of Object.keys(obj)) {
strMap.set(k, obj[k])
}
return strMap
},
mapToJson(map) {
return JSON.stringify(strMapToObj(map))
},
jsonToMap(jsonStr) {
return objToStrMap(JSON.parse(jsonStr))
},
22、 判定数据类型
getDataType(object) {
if (typeof object === 'undefined') {
return 'undefined'
}
let map = {
'[object Number]': 'number',
'[object String]': 'string',
'[object Boolean]': 'boolean',
'[object Object]': 'object',
'[object Array]': 'array',
'[object RegExp]': 'regExp',
'[object Function]': 'function',
'[object Promise]': 'promise',
'[object GeneratorFunction]': 'generatorFunction',
'[object Null]': 'null',
}
let typeString = ''
if (object instanceof Element) {
typeString = 'element'
} else {
typeString = map[Object.prototype.toString.call(object)]
}
return typeString
},
23、深拷贝
//深拷贝
deepCopy(obj) {
// Hash表 记录所有的对象引用关系
let map = new WeakMap()
function dp(obj) {
let result = null
let keys = null,
key = null,
temp = null,
existObj = null
existObj = map.get(obj)
// 如果这个对象已被记录则直接返回
if (existObj) {
return existObj
}
keys = Object.keys(obj)
result = {}
// 记录当前对象
map.set(obj,result)
for (let i = 0
key = keys[i]
temp = obj[key]
// 如果字段的值也是一个对象则递归复制
if (temp && (Object.prototype.toString.call(temp) === '[object Array]' || Object.prototype.toString.call(temp) === '[object Object]')) {
result[key] = dp(temp)
} else {
// 否则直接赋值给新对象
result[key] = temp
}
}
return result
}
return dp(obj)
},
24、判定两个对象是否相等,包含子对象(所有键和键值都一致)
compareObject(object1, object2) {
if (typeof (object1) !== 'object' || typeof (object2) !== 'object') {
throw new Error('Please enter object params.')
}
const aProps = Object.keys(object1)
const bProps = Object.keys(object2)
if (aProps.length !== bProps.length) {
return false
}
for (let i = 0; i < aProps.length; i++) {
let propName = aProps[i]
let propA = object1[propName]
let propB = object2[propName]
if (typeof (propA) === 'object') {
if (!this.isObjectValueEqual(propA, propB)) {
return false
}
} else if (propA !== propB) {
return false
}
}
return true
},
26、Json转换成对象
toObject(data) {
let newData = {}
try {
newData = JSON.parse(data)
} catch (e) {
newData = data
}
return newData
},
27、判断是不是ie8\9\10等低版本浏览器
isIELowVersion() {
if (!!window.ActiveXObject || "ActiveXObject" in window) {
if (navigator && navigator.appName === "Microsoft Internet Explorer" &&
(navigator.appVersion.match(/8./i) || navigator.appVersion.match(/9./i) || navigator.appVersion.match(/10./i))) {
return true
}
}
return false
}
28、判断是不是ie11浏览器
isIE11() {
if (!!window.ActiveXObject || "ActiveXObject" in window) {
if (navigator && navigator.appVersion.match(/11./i)) {
return true;
}
}
return false;
},