element 中 MessageBox 已經(jīng)是封裝好了的。但是根據(jù)實際項目中的需要,彈框的類型肯定是不止一種的。
這里以 MessageBox 中的 confirm 為例。
@util/utils.js 中
/**
* @param content 彈框中提示的內(nèi)容
* @param callback 點擊確認后返回的函數(shù)
* @param btnText 確認按鈕,很多情況下確認按鈕都需要有單獨的文案。同理還可以增加取消按鈕的文案。
* @param type 消息類型,用于顯示圖標 success / info / warning / error
*/
import ElementUI from "element-ui"; //這里是引入 element
const utils = {
alertConfirmReminder(content, callback, btnText, type) {
ElementUI.MessageBox.confirm(content,
"溫馨提示", {
confirmButtonText: btnText || "確定",
cancelButtonText: "取消",
type: type
}).then(() => {
callback()
})
.catch(() => {});
},
}
export default utils
main.js中全局引入工具庫
import Utils from '@/plugin/utils'
Vue.prototype.$utils = window.$utils = Utils
如在頁面中使用
this.$utils.alertConfirmReminder("確定要刷新緩存嗎?", function () {
// 執(zhí)行一些刷新需要的邏輯
}, "刷新", 'warning');