Vue中util的工具函数实例详解

时间:2021-05-26

Vue中util的工具函数,下面通过实例代码给大家介绍的非常详细,具体代码如下所示:

// 防抖函数function debounce (fn, wait) {let treturn () => {let context = thislet args = argumentsif (t) clearTimeout(t)t = setTimeout(() => { fn.apply(context, args)}, wait)}}function flatten (arr) { // 数组扁平化return arr.reduce((result, item) => {return result.concat(Array.isArray(item) ? flatten(item) : item)}, [])}function handleMulitePerson (sPerson) {console.log(44, sPerson.split(','))if (typeof sPerson == 'string') {let personArr = []sPerson.split(',').forEach(item => { let obj = { userCode: item.split('/')[0], userName: item.split('/')[1] } personArr.push(obj)})return personArr}}function uniqueArray (array, key) { // json数组根据key去重var result = [array[0]]for (var i = 1; i < array.length; i++) {var item = array[i]var repeat = falsefor (var j = 0; j < result.length; j++) { if (item[key] == result[j][key]) { repeat = true break }}if (!repeat) { result.push(item)}}return result}function setSessionStorage (key, val) {if (typeof (val) == 'object') {sessionStorage.setItem(key, JSON.stringify(val))} else {sessionStorage.setItem(key, val)}}function getSessionStorage (key) {return sessionStorage.getItem(key)}function removeStorage (key) {sessionStorage.removeItem(key)}function urlParams () { // 获取链接参数var str = location.search.length > 0 ? location.search.substring(1) : ''var items = str.length ? str.split('&') : []var args = {}var item = nullvar name = nullvar value = nullfor (let i = 0, len = items.length; i < len; i++) {item = items[i].split('=')name = decodeURIComponent(item[0])value = decodeURIComponent(item[1])if (name.length) { args[name] = value}};return args}function urlAfterParams () {var str = window.location.hash.length > 0 ? window.location.hash.substring(window.location.hash.indexOf('?') + 1) : ''var items = str.indexOf('&') > 0 ? str.split('&') : str.split('?')var args = {}var item = nullvar name = nullvar value = nullfor (let i = 0, len = items.length; i < len; i++) {item = items[i].split('=')name = decodeURIComponent(item[0])value = decodeURIComponent(item[1])if (name.length) { args[name] = value}};return args}function parseParams (url) {url = decodeURIComponent(url)var params = {}var idx = url.indexOf('?')if (idx > 0) {var queryStr = url.substring(idx + 1)if (queryStr.length > 0) { var arr = queryStr.split('&') for (let i = 0; i < arr.length; i++) { var pair = arr[i].split('=') if (pair.length == 2 && pair[0].length > 0) { params[pair[0]] = pair[1] } }}}return params}/**选人下拉框数据:username(userCode)*/function getSelectUserName (userName, userCode) {return userName + '(' + userCode + ')'}function getSelectLoginUser () {var userInfo = getLoginUserInfo()return getSelectUserName(userInfo.userName, userInfo.userId)}function getUserNameBySelectUserName (userName) {let i = userName.indexOf('(')return userName.substring(0, i)}/**登录用户信息userIduserNamemobileNo@returns {any}*/function getLoginUserInfo () {return JSON.parse(localStorage.getItem('userInfo'))}function getLoginUserCode () {return JSON.parse(localStorage.getItem('userInfo')).userId}export default {getNyr,getYDate,setSessionStorage,urlParams,urlAfterParams,parseParams,debounce,handleMulitePerson,uniqueArray,flatten,getSessionStorage,removeStorage,getSelectUserName,getSelectLoginUser,getLoginUserInfo,getLoginUserCode,getUserNameBySelectUserName}

总结

以上所述是小编给大家介绍的Vue中util的工具函数实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章