vue修改this.$confirm的文字樣式、自定義樣式代碼實(shí)例
通常使用 confirm 確認(rèn)框時,一般這樣寫:
<template> <el-button type="text" @click="open">點(diǎn)擊打開 Message Box</el-button> </template> <script> export default { methods: { open() { this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }).then(() => { this.$message({ type: 'success', message: '刪除成功!' }); }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }); }); } } } </script>
但偶爾也需要修改文字等樣式,這時該怎么做呢?
一、 將dangerouslyUseHTMLString屬性設(shè)置為 true,message 就會被當(dāng)作 HTML 片段處理。
提示文字中,修改部分字體樣式時,可以這樣做:
<template> <el-button type="text" @click="open">點(diǎn)擊打開 Message Box</el-button> </template> <script> export default { methods: { open() { this.$confirm('此操作將<span style="color: red;">永久刪除</span>該文件, 是否繼續(xù)?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', dangerouslyUseHTMLString: true, // 使用HTML片段 type: 'warning' }).then(() => { this.$message({ type: 'success', message: '刪除成功!' }); }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }); }); } } } </script>
二、createElement 新建元素和對象,然后對新建的元素進(jìn)行標(biāo)簽化設(shè)置。
<template> <el-button type="text" @click="open">點(diǎn)擊打開 Message Box</el-button> </template> <script> export default { methods: { open() { const h = this.$createElement this.$confirm( '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning', message:h('p', null, [ h('span', null, '內(nèi)容可以是 '), h('i', { style: 'color: red' }, 'xxxxx') ]), // iconClass:"el-icon-question colorYellow", // 需要修改 icon 圖標(biāo),需要把注釋代碼打開,其中 colorYellow 表示圖標(biāo)顏色,(自定義圖標(biāo)的類名,會覆蓋 type) }).then(() => { this.$message({ type: 'success', message: '刪除成功!' }); }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }); }); } } } </script>
設(shè)置 iconClass 屬性,可以更改icon:
三、文字換行顯示
<template> <el-button type="text" @click="open">點(diǎn)擊打開 Message Box</el-button> </template> <script> export default { methods: { open() { const confirmText = ['第一行內(nèi)容', '第二行內(nèi)容','第三行內(nèi)容'] const newData = [] const h = this.$createElement for (const i in confirmText) { newData.push(h('p', null, confirmText[i])) } this.$confirm( '提示', { title:'提示', confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning', message: h('div', null, newData), }).then(() => { this.$message({ type: 'success', message: '刪除成功!' }); }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }); }); } } } </script>
四、使用 customClass 設(shè)置MessageBox 的自定義類名,從而自定義樣式
<template> <el-button type="text" @click="open">點(diǎn)擊打開 Message Box</el-button> </template> <script> export default { methods: { open() { this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning', customClass:'del-model', // 設(shè)置MessageBox 的自定義類名 }).then(() => { this.$message({ type: 'success', message: '刪除成功!' }); }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }); }); } } } </script> //注意這里不能將樣式放到scoped中!?。? <style lang="scss"> .del-model { .el-button:nth-child(1) { width: 100px;//修改確認(rèn)按鈕的寬度 } .el-button:nth-child(2) { margin-right: 10px; background-color: #2d8cf0; border-color: #2d8cf0; } } </style>
附:vue element插件this.$confirm用法(取消也可以發(fā)請求)
場景:彈出框的兩個按鈕都能分別請求接口
最簡單的彈出框就是“確定”“取消”,一般用戶點(diǎn)擊確定才會繼續(xù)接下來的動作,點(diǎn)擊取消則不做任何動作(即不會請求接口)。
如:
<template> <el-button type="text" @click="open">點(diǎn)擊打開 Message Box</el-button> </template> <script> export default { methods: { open() { this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', { confirmButtonText: '確定', cancelButtonText: '取消', type: 'warning' }).then(() => { this.$message({ type: 'success', message: '刪除成功!' }); }).catch(() => { this.$message({ type: 'info', message: '已取消刪除' }); }); } } } </script>
兩個按鈕都請求,則:
//任務(wù)下線 offline(data){ this.$confirm('是否開啟保存點(diǎn)?', { distinguishCancelAndClose: true, confirmButtonText: '是', cancelButtonText: '否', //相當(dāng)于 取消按鈕 type: 'warning' }).then(() => { api.taskOffline({taskId: data.taskId, isSavepoint: '1'}).then(res => { if (res.data.code === "100") { this.$message({type: 'success', message: '下線成功!'}) this.getTableData() } else { this.$message({type: 'error', message: res.data.msg}) this.getTableData() } }) }).catch(action => { //判斷是 cancel (自定義的取消) 還是 close (關(guān)閉彈窗) if (action === 'cancel'){ api.taskOffline({taskId: data.taskId, isSavepoint: '0'}).then(res => { if (res.data.code === "100") { this.$message({type: 'success', message: '下線成功!'}) this.getTableData() } else { this.$message({type: 'error', message: res.data.msg}) this.getTableData() } }) } })
默認(rèn)情況下,當(dāng)用戶觸發(fā)取消(點(diǎn)擊取消按鈕)和觸發(fā)關(guān)閉(點(diǎn)擊關(guān)閉按鈕或遮罩層、按下 ESC 鍵)時,Promise 的 reject 回調(diào)和callback回調(diào)的參數(shù)均為 ‘cancel’(普通彈出框中的點(diǎn)擊取消時的回調(diào)參數(shù))。如果將distinguishCancelAndClose屬性設(shè)置為 true,則上述兩種行為的參數(shù)分別為 ‘cancel’ 和 ‘close’。(注意:如果沒有設(shè)置distinguishCancelAndClose為true,則都默認(rèn)為取消)
這樣就可以在catch中拿到回調(diào)參數(shù)action進(jìn)行判斷做什么操作了
總結(jié)
到此這篇關(guān)于vue修改this.$confirm的文字樣式、自定義樣式的文章就介紹到這了,更多相關(guān)vue修改this.$confirm文字樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue解決element-ui消息提示$message重疊問題
這篇文章主要為大家介紹了Vue解決element-ui消息提示$message重疊問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Vue3+Vite中不支持require的方式引入本地圖片的解決方案
這篇文章主要介紹了Vue3+Vite中不支持require的方式引入本地圖片的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Vue利用Blob下載原生二進(jìn)制數(shù)組文件
這篇文章主要為大家詳細(xì)介紹了Vue利用Blob下載原生二進(jìn)制數(shù)組文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能
這篇文章主要介紹了Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06Vue Element前端應(yīng)用開發(fā)之功能點(diǎn)管理及權(quán)限控制
在一個業(yè)務(wù)管理系統(tǒng)中,如果我們需要實(shí)現(xiàn)權(quán)限控制功能,我們需要定義好對應(yīng)的權(quán)限功能點(diǎn),然后在界面中對界面元素的功能點(diǎn)進(jìn)行綁定,這樣就可以在后臺動態(tài)分配權(quán)限進(jìn)行動態(tài)控制了,權(quán)限功能點(diǎn)是針對角色進(jìn)行控制的,也就是簡稱RBAC(Role Based Access Control)。2021-05-05vue computed計算屬性顯示undefined的解決
這篇文章主要介紹了vue computed計算屬性顯示undefined的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11vue 判斷兩個時間插件結(jié)束時間必選大于開始時間的代碼
這篇文章主要介紹了vue 判斷兩個時間插件結(jié)束時間必選大于開始時間的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11在vue-cli3.0 中使用預(yù)處理器 (Sass/Less/Stylus) 配置全局變量操作
這篇文章主要介紹了在vue-cli3.0 中使用預(yù)處理器 (Sass/Less/Stylus) 配置全局變量操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08