摘要:項(xiàng)目中工具函數(shù),我們通常會(huì)添加到的原型中,這樣就實(shí)現(xiàn)了全局函數(shù)只需要將綁定的這段引入到即可。對(duì)象中可以有兩個(gè)屬性和是布爾值,為真時(shí),不會(huì)對(duì)獲取到的值進(jìn)行解碼。參數(shù)可選,可以有以下屬性字符串字符串?dāng)?shù)值或日期對(duì)象布爾值。持續(xù)更新參考工具函數(shù)
Vue 項(xiàng)目中工具函數(shù),我們通常會(huì)添加到Vue的原型中,這樣就實(shí)現(xiàn)了全局函數(shù)import Vue from "vue"
Vue.prototype.$tools = function (){}
只需要將綁定的這段js引入到main.js即可。綁定方法十分簡(jiǎn)單,這里不再詳細(xì)說(shuō)明
下面列舉幾個(gè)常用的工具函數(shù)
$dateFormat 事件格式化函數(shù),相對(duì)于moment輕很多Vue.prototype.$dateFormat = function (date, fmt = "YYYY-MM-DD HH:mm:ss") {
if (!date) {
return ""
}
if (typeof date === "string") {
date = new Date(date.replace(/-/g, "/"))
}
if (typeof date === "number") {
date = new Date(date)
}
var o = {
"M+": date.getMonth() + 1,
"D+": date.getDate(),
"h+": date.getHours() % 12 === 0 ");"H+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
"q+": Math.floor((date.getMonth() + 3) / 3),
"S": date.getMilliseconds()
}
var week = {
"0": "u65e5",
"1": "u4e00",
"2": "u4e8c",
"3": "u4e09",
"4": "u56db",
"5": "u4e94",
"6": "u516d"
}
if (/(Y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ");$1.length > 2 ");"u661fu671f" : "u5468") : "") + week[date.getDay() + ""])
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ");"00" + o[k]).substr(("" + o[k]).length)))
}
}
return fmt
}
$ajax
import axios from "axios"
// 跨域請(qǐng)求,允許保存cookieaxios.defaults.withCredentials = true
// HTTPrequest攔截,對(duì)發(fā)出的請(qǐng)求數(shù)據(jù)進(jìn)行統(tǒng)一操作
axios.interceptors.request.use(config => {
// 對(duì)請(qǐng)求參數(shù)做些什么
return config
}, error => {
// 對(duì)請(qǐng)求錯(cuò)誤做些什么
console.log("err" + error) // for debug
return Promise.reject(error)
})
// HTTPresponse攔截,對(duì)收到的數(shù)據(jù)進(jìn)行統(tǒng)一操作
axios.interceptors.response.use(data => {
// 對(duì)返回?cái)?shù)據(jù)進(jìn)行操作
return data
}, error => {
return Promise.reject(new Error(error))
})
Vue.prototype.$ajax = function ajax (url = "", data = {}, type = "GET") {
type = type.toUpperCase()
return new Promise(function (resolve, reject) {
let promise
if (type === "GET") {
let dataStr = "" // 數(shù)據(jù)拼接字符串
Object.keys(data).forEach(key => {
dataStr += key + "=" + data[key] + "&"
})
if (dataStr !== "") {
dataStr = dataStr.substr(0, dataStr.lastIndexOf("&"))
url = url + ""); + dataStr
}
// 發(fā)送get請(qǐng)求
promise = axios.get(url)
} else {
// 發(fā)送post
promise = axios.post(url, data)
}
promise.then(response => {
resolve(response.data)
}).catch(error => {
reject(error)
})
})
}
數(shù)字格式化
numberComma用于分割數(shù)字,默認(rèn)為3位分割,一般用于格式化金額。
Vue.prototype.$numberComma = function (source, length = 3) {
source = String(source).split(".")
source[0] = source[0].replace(new RegExp("(d)("); + length + "})+$)", "ig"), "$1,")
return source.join(".")
}
數(shù)字補(bǔ)位
numberPad用于按照位數(shù)補(bǔ)0,默認(rèn)為2
Vue.prototype.$numberPad = function (source, length = 2) {
let pre = ""
const negative = source < 0
const string = String(Math.abs(source))
if (string.length < length) {
pre = (new Array(length - string.length + 1)).join("0")
}
return (negative ");"-" : "") + pre + string
}
取隨機(jī)數(shù)字
Vue.prototype.$numberRandom = function (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
cookie操作
1.$cookie.get(name, [options])
獲取 cookie 值。options 參數(shù)可選,取值如下:
converter 轉(zhuǎn)換函數(shù)。如果所獲取的 cookie 有值,會(huì)在返回前傳給 converter
函數(shù)進(jìn)行轉(zhuǎn)換。
選項(xiàng)對(duì)象。對(duì)象中可以有兩個(gè)屬性:converter 和 raw. raw 是布爾值,為真時(shí),不會(huì)對(duì)獲取到的
cookie 值進(jìn)行 URI 解碼。
注:如果要獲取的 cookie 鍵值不存在,則返回 undefined.
2.cookie.remove(name, [options])
移除指定的 cookie.
const Cookie = {}
var decode = decodeURIComponent
var encode = encodeURIComponent
Cookie.get = function (name, options) {
validateCookieName(name)
if (typeof options === "function") {
options = {
converter: options
}
} else {
options = options || {}
}
var cookies = parseCookieString(document.cookie, !options["raw"])
return (options.converter || same)(cookies[name])
}
Cookie.set = function (name, value, options) {
validateCookieName(name)
options = options || {}
var expires = options["expires"]
var domain = options["domain"]
var path = options["path"]
if (!options["raw"]) {
value = encode(String(value))
}
var text = name + "=" + value
// expires
var date = expires
if (typeof date === "number") {
date = new Date()
date.setDate(date.getDate() + expires)
}
if (date instanceof Date) {
text += " expires=" + date.toUTCString()
}
// domain
if (isNonEmptyString(domain)) {
text += " domain=" + domain
}
// path
if (isNonEmptyString(path)) {
text += " path=" + path
}
// secure
if (options["secure"]) {
text += " secure"
}
document.cookie = text
return text
}
Cookie.remove = function (name, options) {
options = options || {}
options["expires"] = new Date(0)
return this.set(name, "", options)
}
function parseCookieString (text, shouldDecode) {
var cookies = {}
if (isString(text) && text.length > 0) {
var decodeValue = shouldDecode ");for (var i = 0, len = cookieParts.length; i < len; i++) {
cookieNameValue = cookieParts[i].match(/([^=]+)=/i)
if (cookieNameValue instanceof Array) {
try {
cookieName = decode(cookieNameValue[1])
cookieValue = decodeValue(cookieParts[i]
.substring(cookieNameValue[1].length + 1))
} catch (ex) {
console.log(ex)
}
} else {
cookieName = decode(cookieParts[i])
cookieValue = ""
}
if (cookieName) {
cookies[cookieName] = cookieValue
}
}
}
return cookies
}
function isString (o) {
return typeof o === "string"
}
function isNonEmptyString (s) {
return isString(s) && s !== ""
}
function validateCookieName (name) {
if (!isNonEmptyString(name)) {
throw new TypeError("Cookie name must be a non-empty string")
}
}
function same (s) {
return s
}
Vue.prototype.$cookie = Cookie
獲取URL中的請(qǐng)求參數(shù)
``` $dateFormat(url) 返回搜索參數(shù)的鍵值對(duì)對(duì)象 例: getsearch("http://www.longfor.com");
Vue.prototype.$getsearch = function (url) {
var obj = {} url.replace(/([^");
#小數(shù)截取固定位數(shù)
// 默認(rèn)保留一位小數(shù),并下舍入
$decimalNum 截取固定位數(shù)的小數(shù)
/**@param
number 處理的小數(shù)
number 保留的小數(shù)位數(shù)
number 0 對(duì)小數(shù)進(jìn)行下舍入;1 四舍五入;2 上舍入
*/
例: $decimalNum(2.376186679,3,)
// 2.376
Vue.prototype.$decimalNum = function (source, length = 1, type = 0) {
length = Math.pow(10, length)
if (type === 2) {
return Math.ceil(source * length) / length
} else if (type === 1) {
return Math.round(source * length) / length
} else {
return Math.floor(source * length) / length
}
}
。。。持續(xù)更新
參考:vux工具函數(shù)
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/6871.html
摘要:案例持續(xù)觸發(fā)事件時(shí),并不立即執(zhí)行函數(shù),當(dāng)毫秒內(nèi)沒(méi)有觸發(fā)事件時(shí),才會(huì)延時(shí)觸發(fā)一次函數(shù)。也以函數(shù)形式暴露普通插槽。這樣的場(chǎng)景組件用函數(shù)式組件是非常方便的。相關(guān)閱讀函數(shù)式組件自定義指令前言 有echarts使用經(jīng)驗(yàn)的同學(xué)可能遇到過(guò)這樣的場(chǎng)景,在window.onresize事件回調(diào)里觸發(fā)echartsBox.resize()方法來(lái)達(dá)到重繪的目的,resize事件是連續(xù)觸發(fā)的這意味著echarts...
摘要:如何構(gòu)建大型的前端項(xiàng)目搭建好項(xiàng)目的腳手架一般新開(kāi)發(fā)一個(gè)項(xiàng)目時(shí),我們會(huì)首先搭建好一個(gè)腳手架,然后才會(huì)開(kāi)始寫(xiě)代碼。組件化一般分為項(xiàng)目?jī)?nèi)的組件化和項(xiàng)目外的組件化。 如何構(gòu)建大型的前端項(xiàng)目 1. 搭建好項(xiàng)目的腳手架 一般新開(kāi)發(fā)一個(gè)項(xiàng)目時(shí),我們會(huì)首先搭建好一個(gè)腳手架,然后才會(huì)開(kāi)始寫(xiě)代碼。一般腳手架都應(yīng)當(dāng)有以下的幾個(gè)功能: 自動(dòng)化構(gòu)建代碼,比如打包、壓縮、上傳等功能 本地開(kāi)發(fā)與調(diào)試,并有熱替換與...
摘要:如何構(gòu)建大型的前端項(xiàng)目搭建好項(xiàng)目的腳手架一般新開(kāi)發(fā)一個(gè)項(xiàng)目時(shí),我們會(huì)首先搭建好一個(gè)腳手架,然后才會(huì)開(kāi)始寫(xiě)代碼。組件化一般分為項(xiàng)目?jī)?nèi)的組件化和項(xiàng)目外的組件化。 如何構(gòu)建大型的前端項(xiàng)目 1. 搭建好項(xiàng)目的腳手架 一般新開(kāi)發(fā)一個(gè)項(xiàng)目時(shí),我們會(huì)首先搭建好一個(gè)腳手架,然后才會(huì)開(kāi)始寫(xiě)代碼。一般腳手架都應(yīng)當(dāng)有以下的幾個(gè)功能: 自動(dòng)化構(gòu)建代碼,比如打包、壓縮、上傳等功能 本地開(kāi)發(fā)與調(diào)試,并有熱替換與...
摘要:從到再到搭建編寫(xiě)構(gòu)建一個(gè)前端項(xiàng)目選擇現(xiàn)成的項(xiàng)目模板還是自己搭建項(xiàng)目骨架搭建一個(gè)前端項(xiàng)目的方式有兩種選擇現(xiàn)成的項(xiàng)目模板自己搭建項(xiàng)目骨架。使用版本控制系統(tǒng)管理源代碼項(xiàng)目搭建好后,需要一個(gè)版本控制系統(tǒng)來(lái)管理源代碼。 從 0 到 1 再到 100, 搭建、編寫(xiě)、構(gòu)建一個(gè)前端項(xiàng)目 1. 選擇現(xiàn)成的項(xiàng)目模板還是自己搭建項(xiàng)目骨架 搭建一個(gè)前端項(xiàng)目的方式有兩種:選擇現(xiàn)成的項(xiàng)目模板、自己搭建項(xiàng)目骨架。 ...
摘要:從到再到搭建編寫(xiě)構(gòu)建一個(gè)前端項(xiàng)目選擇現(xiàn)成的項(xiàng)目模板還是自己搭建項(xiàng)目骨架搭建一個(gè)前端項(xiàng)目的方式有兩種選擇現(xiàn)成的項(xiàng)目模板自己搭建項(xiàng)目骨架。使用版本控制系統(tǒng)管理源代碼項(xiàng)目搭建好后,需要一個(gè)版本控制系統(tǒng)來(lái)管理源代碼。 從 0 到 1 再到 100, 搭建、編寫(xiě)、構(gòu)建一個(gè)前端項(xiàng)目 1. 選擇現(xiàn)成的項(xiàng)目模板還是自己搭建項(xiàng)目骨架 搭建一個(gè)前端項(xiàng)目的方式有兩種:選擇現(xiàn)成的項(xiàng)目模板、自己搭建項(xiàng)目骨架。 ...
閱讀 3913·2021-11-24 09:38
閱讀 3248·2021-11-15 11:37
閱讀 864·2021-11-12 10:36
閱讀 3622·2021-10-21 09:38
閱讀 3303·2021-09-28 09:36
閱讀 2504·2021-09-22 16:01
閱讀 5158·2021-09-22 15:09
閱讀 1316·2019-08-30 15:55