1.實(shí)用性vue技巧
1.1 vue的輸入框格式化和校驗(yàn)
- 1 整字格式
在v-model后面加.number即可實(shí)現(xiàn)只輸入整數(shù)
- 2 小數(shù)格式
在input組件或標(biāo)簽里加 一下代碼即可實(shí)現(xiàn)輸入格式化的小數(shù)格式
onKeyUp="value = value.replace(/[^\d.]/g,'');value = value.replace(/\.{2,}/g,'.');
value = value.replace(/^\./g,'');value = value.replace('.','$#$').replace(/\./g,'').replace('$#$','.');"
^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$ //身份證
^([A-Z]\d{6,10}(\(\w{1}\))?)$ //港澳
^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$
^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})
1.2 axios下載文件
axios.post(url,params,{responseType: 'blob',// 返回類型設(shè)置 指定為文件
headers: {'Content-Type':'application/json;application/octet-stream'}, // 頭部設(shè)置
}).then((response) => {
const fileName = decodeURI(response.headers['content-disposition'].split('=')[1]) // 獲取下載文件的文件名 有些接口可能沒(méi)有
let blob = new Blob([response.data], { // 用blob接收返回的文件流
type: "application/zip" // 解析文件的格式 zip壓縮包闯狱、vnd.ms-excel表格
})
// 下載函數(shù)
function downloadFile(url, name) {
var a = document.createElement("a");
a.target = "_blank"
a.href = url;
a.setAttribute("download", name || url || '');
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
a.dispatchEvent(evt);
}
downloadFile(URL.createObjectURL(blob), fileName)
}, (error) => {
// 請(qǐng)求異常
this.$alert('下載異常')
})
1.3 文件引入
- 1 在template里
--- 多用require+@引入圖片或文件
- 2 在script里
--- 導(dǎo)入用import+@引入
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者