代码主体
import Vue from 'vue';
import { escapeToHtml } from '@/util/htmlAndEscape';
function confirmBox({ info = '此操作将永久删除该信息, 是否继续?', title = '提示', bts = ['确定', '取消'], type = 'warning', okFun, noFun }) {
Vue.prototype.$confirm(info, title, {
confirmButtonText: bts[0],
cancelButtonText: bts[1],
type: type
})
.then(() => {
okFun();
})
.catch(() => {
noFun();
});
}
function messageBox({ message = '', type = 'success' }) {
Vue.prototype.$message({
message: message,
type: type
});
}
function dateFormat(fmt, date) {
let ret;
const opt = {
'Y+': date.getFullYear().toString(),
'm+': (date.getMonth() + 1).toString(),
'd+': date.getDate().toString(),
'H+': date.getHours().toString(),
'M+': date.getMinutes().toString(),
'S+': date.getSeconds().toString()
};
for (const k in opt) {
ret = new RegExp('(' + k + ')').exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
}
}
return fmt;
}
function timeFormat(time) {
return time;
}
function delHtmlTag(cc) {
escapeToHtml(cc);
return escapeToHtml(cc).replace(/<[^>]+>/g, '').replace(/ /g, '');
}
export {
confirmBox,
messageBox,
dateFormat,
timeFormat,
delHtmlTag
};
封装的htmlAndEscape.js
function escapeToHtml(str) {
const arrEntities = { 'lt': '<', 'gt': '>', "'": '"' };
return str.replace(/&(lt|gt);/ig, function(all, t) { return arrEntities[t]; });
}
function htmlToEscape(myHtml) {
return myHtml.replace(/[<>"]/g, function(c) { return { '<': '<', '>': '>', '"': "'" }[c]; });
}
export {
escapeToHtml,
htmlToEscape
};
官网链接:https://element.eleme.cn/#/zh-CN/component/message-box