vue修改this.$confirm的文字樣式、自定義樣式代碼實(shí)例
通常使用 confirm 確認(rèn)框時(shí),一般這樣寫:
<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>
但偶爾也需要修改文字等樣式,這時(shí)該怎么做呢?
一、 將dangerouslyUseHTMLString屬性設(shè)置為 true,message 就會(huì)被當(dāng)作 HTML 片段處理。
提示文字中,修改部分字體樣式時(shí),可以這樣做:

<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 新建元素和對(duì)象,然后對(duì)新建的元素進(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)的類名,會(huì)覆蓋 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ā)請(qǐng)求)
場(chǎng)景:彈出框的兩個(gè)按鈕都能分別請(qǐng)求接口
最簡(jiǎn)單的彈出框就是“確定”“取消”,一般用戶點(diǎn)擊確定才會(huì)繼續(xù)接下來的動(dòng)作,點(diǎn)擊取消則不做任何動(dòng)作(即不會(huì)請(qǐng)求接口)。
如:
<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>
兩個(gè)按鈕都請(qǐng)求,則:
//任務(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 鍵)時(shí),Promise 的 reject 回調(diào)和callback回調(diào)的參數(shù)均為 ‘cancel’(普通彈出框中的點(diǎn)擊取消時(shí)的回調(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中項(xiàng)目如何提交form格式數(shù)據(jù)的表單
這篇文章主要介紹了vue中項(xiàng)目如何提交form格式數(shù)據(jù)的表單,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue-cli webpack2項(xiàng)目打包優(yōu)化分享
下面小編就為大家分享一篇vue-cli webpack2項(xiàng)目打包優(yōu)化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue3中通過ref獲取元素節(jié)點(diǎn)的實(shí)現(xiàn)
這篇文章主要介紹了vue3中通過ref獲取元素節(jié)點(diǎn)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue和relation-graph庫打造高質(zhì)量的關(guān)系圖應(yīng)用程序
這篇文章主要介紹了Vue和relation-graph庫打造高質(zhì)量的關(guān)系圖應(yīng)用程序,在這篇文章中,我們深入探討了如何使用Vue和relation-graph高效打造關(guān)系圖,我們?cè)敿?xì)介紹了數(shù)據(jù)準(zhǔn)備、關(guān)系映射、點(diǎn)擊事件等關(guān)鍵步驟,需要的朋友可以參考下2023-09-09
關(guān)于elementUi表格合并行數(shù)據(jù)并展示序號(hào)
這篇文章主要介紹了關(guān)于elementUi表格合并行數(shù)據(jù)并展示序號(hào),通過給table傳入span-method方法可以實(shí)現(xiàn)合并行或列,方法的參數(shù)是一個(gè)對(duì)象,感興趣的朋友可以學(xué)習(xí)一下2023-04-04
Vue下滾動(dòng)到頁面底部無限加載數(shù)據(jù)的示例代碼
本篇文章主要介紹了Vue下滾動(dòng)到頁面底部無限加載數(shù)據(jù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
vue2和el-input無法修改和寫入并且不報(bào)錯(cuò)的解決方案
這篇文章主要介紹了vue2和el-input無法修改和寫入并且不報(bào)錯(cuò)的解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
Vue路由的模塊自動(dòng)化與統(tǒng)一加載實(shí)現(xiàn)
這篇文章主要介紹了Vue路由的模塊自動(dòng)化與統(tǒng)一加載實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
vue實(shí)現(xiàn)配置全局訪問路徑頭(axios)
今天小編就為大家分享一篇vue實(shí)現(xiàn)配置全局訪問路徑頭(axios),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11

