SoFunction
Updated on 2025-03-08

Detailed explanation of the tool function example of util in Vue

The tool function of util in Vue is introduced to you in detail through the example code. The specific code is as follows:

// Anti-shake functionfunction debounce (fn, wait) {
let t
return () => {
let context = this
let args = arguments
if (t) clearTimeout(t)
t = setTimeout(() => {
 (context, args)
}, wait)
}
}
function flatten (arr) { // Array flatteningreturn ((result, item) => {
return ((item) ? flatten(item) : item)
}, [])
}
function handleMulitePerson (sPerson) {
(44, (','))
if (typeof sPerson == 'string') {
let personArr = []
(',').forEach(item => {
 let obj = { userCode: ('/')[0], userName: ('/')[1] }
 (obj)
})
return personArr
}
}
function uniqueArray (array, key) { // json array is re-registered according to keyvar result = [array[0]]
for (var i = 1; i < ; i++) {
var item = array[i]
var repeat = false
for (var j = 0; j < ; j++) {
 if (item[key] == result[j][key]) {
  repeat = true
  break
 }
}
if (!repeat) {
 (item)
}
}
return result
}
function setSessionStorage (key, val) {
if (typeof (val) == 'object') {
(key, (val))
} else {
(key, val)
}
}
function getSessionStorage (key) {
return (key)
}
function removeStorage (key) {
(key)
}
function urlParams () { // Get link parametersvar str =  > 0 ? (1) : ''
var items =  ? ('&') : []
var args = {}
var item = null
var name = null
var value = null
for (let i = 0, len = ; i < len; i++) {
item = items[i].split('=')
name = decodeURIComponent(item[0])
value = decodeURIComponent(item[1])
if () {
 args[name] = value
}
};
return args
}
function urlAfterParams () {
var str =  > 0 ? (('?') + 1) : ''
var items = ('&') > 0 ? ('&') : ('?')
var args = {}
var item = null
var name = null
var value = null
for (let i = 0, len = ; i < len; i++) {
item = items[i].split('=')
name = decodeURIComponent(item[0])
value = decodeURIComponent(item[1])
if () {
 args[name] = value
}
};
return args
}
function parseParams (url) {
url = decodeURIComponent(url)
var params = {}
var idx = ('?')
if (idx > 0) {
var queryStr = (idx + 1)
if ( > 0) {
 var arr = ('&')
 for (let i = 0; i < ; i++) {
  var pair = arr[i].split('=')
  if ( == 2 && pair[0].length > 0) {
   params[pair[0]] = pair[1]
  }
 }
}
}
return params
}
/**
 Select person drop-down box data: username(userCode)
 */
function getSelectUserName (userName, userCode) {
return userName + '(' + userCode + ')'
}
function getSelectLoginUser () {
var userInfo = getLoginUserInfo()
return getSelectUserName(, )
}
function getUserNameBySelectUserName (userName) {
let i = ('(')
return (0, i)
}
/**
 Login user information
 userId
 userName
 mobileNo
 @returns {any}
 */
function getLoginUserInfo () {
return (('userInfo'))
}
function getLoginUserCode () {
return (('userInfo')).userId
}
export default {
getNyr,
getYDate,
setSessionStorage,
urlParams,
urlAfterParams,
parseParams,
debounce,
handleMulitePerson,
uniqueArray,
flatten,
getSessionStorage,
removeStorage,
getSelectUserName,
getSelectLoginUser,
getLoginUserInfo,
getLoginUserCode,
getUserNameBySelectUserName
}

Summarize

The above is a detailed explanation of the tool function examples of util in Vue introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!