vue前端常用的工具類總結(jié)
utils文件夾中的util.js編寫公共工具類
const initUtil = {}
Byte 轉(zhuǎn) KB/MB/GB
initUtil.getfilesize = (size = 0,) => { if (!size) return '0.00GB'; const num = 1000.00; //byte if (size < num) return size + "B"; if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + "KB"; //kb if (size < Math.pow(num, 3)) return (size / Math.pow(num, 2)).toFixed(2) + "MB"; //M if (size < Math.pow(num, 4)) return (size / Math.pow(num, 3)).toFixed(2) + "GB"; //G }
知識(shí)點(diǎn):
1、Math.pow(base, exponent):Math.pow(2, 3),2 的 3 次方是 8。
2、toFixed(2) 格式化數(shù)字,返回字符串類型,當(dāng)前保留數(shù)字后兩位
獲取url指定參數(shù)
使用 URLSearchParams 對象:
initUtil.getUrlParam = (name) => { // 假設(shè) URL 為:https://example.com/page?name=John&age=25 // 創(chuàng)建 URLSearchParams 對象,將 URL 查詢參數(shù)解析到該對象中 const urlParams = new URLSearchParams(window.location.search); // 獲取指定參數(shù)的值 return urlParams.get(name) }
使用正則表達(dá)式:
initUtil.getUrlParam = (name,url) => { name = name.replace(/[\[\]]/g, "\\$&"); const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url || window.location.href); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); } getUrlParam('name')
知識(shí)點(diǎn)
1、正則表達(dá)式(Regular Expression): 正則表達(dá)式用于在 URL 中匹配指定的參數(shù)名,并捕獲對應(yīng)的參數(shù)值。 name = name.replace(/[[]]/g, “\$&”); 這一行用于將參數(shù)名中的方括號(hào)進(jìn)行轉(zhuǎn)義,確保正則表達(dá)式匹配正確。
2、RegExp 對象: const regex = new RegExp(“[?&]” + name + “(=([^&#]*)|&|#|$)”); 創(chuàng)建了一個(gè)正則表達(dá)式對象,該正則表達(dá)式匹配 URL 查詢字符串中指定參數(shù)名的模式。
3、exec 方法: regex.exec(url || window.location.href); 使用 exec 方法在URL中執(zhí)行正則表達(dá)式,返回匹配的結(jié)果數(shù)組。結(jié)果數(shù)組中,results[0] 包含整個(gè)匹配項(xiàng),results[2] 包含參數(shù)值
日期格式化
initUtil.dateFormat = (date, format = 'YYYY-MM-DD HH:mm:ss') => { const config = { YYYY: date.getFullYear(), MM: date.getMonth() + 1,//getMonth() 方法根據(jù)本地時(shí)間返回指定日期的月份(從 0 到 11) DD: date.getDate(), HH: date.getHours(), mm: date.getMinutes(), ss: date.getSeconds(), } for (const key in config) { format = format.replace(key, config[key]) } return format }
知識(shí)點(diǎn):
replace方法的基本語法是:
newString = originalString.replace(searchValue, replaceValue);
originalString 是原始字符串。
searchValue 是要被替換的子字符串或正則表達(dá)式。
replaceValue 是替換的內(nèi)容。
如果 searchValue 是一個(gè)字符串,只會(huì)替換第一次出現(xiàn)的匹配項(xiàng)。
如果 searchValue 是一個(gè)正則表達(dá)式,會(huì)替換所有匹配項(xiàng)。
返回的是一個(gè)新的字符串,原始字符串并沒有被改變
以下是一些示例:
let originalString = "Hello, world! Hello, universe!"; let newString = originalString.replace("Hello", "Hi"); console.log(newString); // 輸出: "Hi, world! Hello, universe!" let regex = /Hello/g; // 使用正則表達(dá)式,全局匹配 newString = originalString.replace(regex, "Hi"); console.log(newString); // 輸出: "Hi, world! Hi, universe!"
返回時(shí)間段
initUtil.getTimeState = (time, lang) => { let text = ``; if (time) { // 獲取當(dāng)前小時(shí) let hours = Number(time.split(':')[0]); // 設(shè)置默認(rèn)文字 // 判斷當(dāng)前時(shí)間段 if (lang !== 'en') { if (hours >= 0 && hours <= 11) { text = `上午`; } else { text = `下午`; } } else { if (hours >= 0 && hours <= 11) { text = `AM`; } else { text = `PM`; } } } // 返回當(dāng)前時(shí)間段對應(yīng)的狀態(tài) return text; }
防抖
initUtil.debounce = (fn, delay = 500) => { let timer return function () { clearTimeout(timer) timer = setTimeout(() => { fn.apply(this, arguments) }, delay) } }
節(jié)流
initUtil.throttle = (fn, delay = 500) => { let timer return function () { if (!timer) { clearTimeout(timer) timer = setTimeout(() => { fn.apply(this, arguments) timer = null }, delay) } } }
到此這篇關(guān)于vue前端常用的工具類總結(jié)的文章就介紹到這了,更多相關(guān)vue工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js選中動(dòng)態(tài)綁定的radio的指定項(xiàng)
這篇文章主要介紹了vue.js選中動(dòng)態(tài)綁定的radio的指定項(xiàng),需要的朋友可以參考下2017-06-06easycom模式開發(fā)UNI-APP組件調(diào)用必須掌握的實(shí)用技巧
uni-app基于VUE開發(fā),通常組件的使用都是先安裝,然后全局或者局部引入,注冊,今天通過本文給大家分享easycom模式開發(fā)UNI-APP組件調(diào)用必須掌握的實(shí)用技巧,需要的朋友一起看看吧2021-08-08Vue實(shí)現(xiàn)父子組件傳值其實(shí)不難
這篇文章主要介紹了Vue實(shí)現(xiàn)父子組件傳值其實(shí)不難問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03uni-app項(xiàng)目中引入Vant?UI組件庫完美避坑指南(純凈版)
網(wǎng)上百度uniapp使用vant時(shí),很多答案都是在根路徑下創(chuàng)建文件夾,而且都是基于小程序環(huán)境的,其實(shí)uniapp可以直接使用的,這篇文章主要給大家介紹了關(guān)于uni-app項(xiàng)目中引入Vant?UI組件庫完美避坑指南的相關(guān)資料,需要的朋友可以參考下2024-02-02Vue3在jsx下父子組件實(shí)現(xiàn)使用插槽方式
這篇文章主要介紹了Vue3在jsx下父子組件實(shí)現(xiàn)使用插槽方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10