在保留當(dāng)前頁(yè)面狀態(tài)的情況下,告知用戶并承載相關(guān)操作。
Dialog 彈出一個(gè)對(duì)話框,適合需要定制性更大的場(chǎng)景。
需要設(shè)置 model-value / v-model 屬性,它接收 Boolean,當(dāng)為 true 時(shí)顯示 Dialog。Dialog 分為兩個(gè)部分:body 和 footer,footer 需要具名為 footer 的 slot。title 屬性用于定義標(biāo)題,它是可選的,默認(rèn)值為空。最后,本例還展示了 before-close 的用法。
<template>
<el-button type="text" @click="dialogVisible = true">點(diǎn)擊打開(kāi) Dialog</el-button>
<el-dialog
title="提示"
v-model="dialogVisible"
width="30%"
:before-close="handleClose"
>
<span>這是一段信息</span>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="dialogVisible = false">確 定</el-button>
</span>
</template>
</el-dialog>
</template>
<script>
export default {
data() {
return {
dialogVisible: false,
}
},
methods: {
handleClose(done) {
this.$confirm('確認(rèn)關(guān)閉?')
.then((_) => {
done()
})
.catch((_) => {})
},
},
}
</script>
before-close 僅當(dāng)用戶通過(guò)點(diǎn)擊關(guān)閉圖標(biāo)或遮罩關(guān)閉 Dialog 時(shí)起效。如果你在 footer 具名 slot 里添加了用于關(guān)閉 Dialog 的按鈕,那么可以在按鈕的點(diǎn)擊回調(diào)函數(shù)里加入 before-close 的相關(guān)邏輯。
Dialog 組件的內(nèi)容可以是任意的,甚至可以是表格或表單,下面是應(yīng)用了 Element Plus Table 和 Form 組件的兩個(gè)樣例。
<template>
<el-button type="text" @click="dialogTableVisible = true"
>打開(kāi)嵌套表格的 Dialog</el-button
>
<el-dialog title="收貨地址" v-model="dialogTableVisible">
<el-table :data="gridData">
<el-table-column property="date" label="日期" width="150"></el-table-column>
<el-table-column property="name" label="姓名" width="200"></el-table-column>
<el-table-column property="address" label="地址"></el-table-column>
</el-table>
</el-dialog>
<!-- Form -->
<el-button type="text" @click="dialogFormVisible = true"
>打開(kāi)嵌套表單的 Dialog</el-button
>
<el-dialog title="收貨地址" v-model="dialogFormVisible">
<el-form :model="form">
<el-form-item label="活動(dòng)名稱" :label-width="formLabelWidth">
<el-input v-model="form.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="活動(dòng)區(qū)域" :label-width="formLabelWidth">
<el-select v-model="form.region" placeholder="請(qǐng)選擇活動(dòng)區(qū)域">
<el-option label="區(qū)域一" value="shanghai"></el-option>
<el-option label="區(qū)域二" value="beijing"></el-option>
</el-select>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="dialogFormVisible = false"
>確 定</el-button
>
</span>
</template>
</el-dialog>
</template>
<script>
export default {
data() {
return {
gridData: [
{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
},
{
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
},
{
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
},
{
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
},
],
dialogTableVisible: false,
dialogFormVisible: false,
form: {
name: '',
region: '',
date1: '',
date2: '',
delivery: false,
type: [],
resource: '',
desc: '',
},
formLabelWidth: '120px',
}
},
}
</script>
如果需要在一個(gè) Dialog 內(nèi)部嵌套另一個(gè) Dialog,需要使用 append-to-body 屬性。
正常情況下,我們不建議使用嵌套的 Dialog,如果需要在頁(yè)面上同時(shí)顯示多個(gè) Dialog,可以將它們平級(jí)放置。對(duì)于確實(shí)需要嵌套 Dialog 的場(chǎng)景,我們提供了append-to-body屬性。將內(nèi)層 Dialog 的該屬性設(shè)置為 true,它就會(huì)插入至 body 元素上,從而保證內(nèi)外層 Dialog 和遮罩層級(jí)關(guān)系的正確。
<template>
<el-button type="text" @click="outerVisible = true"
>點(diǎn)擊打開(kāi)外層 Dialog</el-button
>
<el-dialog title="外層 Dialog" v-model="outerVisible">
<el-dialog
width="30%"
title="內(nèi)層 Dialog"
v-model="innerVisible"
append-to-body
>
</el-dialog>
<template #footer>
<div class="dialog-footer">
<el-button @click="outerVisible = false">取 消</el-button>
<el-button type="primary" @click="innerVisible = true"
>打開(kāi)內(nèi)層 Dialog</el-button
>
</div>
</template>
</el-dialog>
</template>
<script>
export default {
data() {
return {
outerVisible: false,
innerVisible: false,
}
},
}
</script>
標(biāo)題和底部可水平居中
將center設(shè)置為true即可使標(biāo)題和底部居中。center僅影響標(biāo)題和底部區(qū)域。Dialog 的內(nèi)容是任意的,在一些情況下,內(nèi)容并不適合居中布局。如果需要內(nèi)容也水平居中,請(qǐng)自行為其添加 CSS。
<template>
<el-button type="text" @click="centerDialogVisible = true"
>點(diǎn)擊打開(kāi) Dialog</el-button
>
<el-dialog title="提示" v-model="centerDialogVisible" width="30%" center>
<span>需要注意的是內(nèi)容是默認(rèn)不居中的</span>
<template #footer>
<span class="dialog-footer">
<el-button @click="centerDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="centerDialogVisible = false"
>確 定</el-button
>
</span>
</template>
</el-dialog>
</template>
<script>
export default {
data() {
return {
centerDialogVisible: false,
}
},
}
</script>
Dialog 的內(nèi)容是懶渲染的,即在第一次被打開(kāi)之前,傳入的默認(rèn) slot 不會(huì)被渲染到 DOM 上。因此,如果需要執(zhí)行 DOM 操作,或通過(guò) ref 獲取相應(yīng)組件,請(qǐng)?jiān)?nbsp;open 事件回調(diào)中進(jìn)行。
可在 Dialog 沒(méi)有顯示時(shí),銷(xiāo)毀 Dialog 里的內(nèi)容以達(dá)到減少 DOM 節(jié)點(diǎn)的作用
需要注意的是,當(dāng)這個(gè)屬性被啟用時(shí),Dialog 內(nèi)并不會(huì)有任何的 DOM 節(jié)點(diǎn)存在,除了 overlay header(如果有) footer(如果有)
<template>
<el-button type="text" @click="centerDialogVisible = true"
>點(diǎn)擊打開(kāi) Dialog</el-button
>
<el-dialog
title="提示"
v-model="centerDialogVisible"
width="30%"
destroy-on-close
center
>
<span>需要注意在 Dialog 打開(kāi)前是這條內(nèi)容和下面的內(nèi)容都是不會(huì)被渲染的</span>
<strong>額外的內(nèi)容</strong>
<template #footer>
<span class="dialog-footer">
<el-button @click="centerDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="centerDialogVisible = false"
>確 定</el-button
>
</span>
</template>
</el-dialog>
</template>
<script>
export default {
data() {
return {
centerDialogVisible: false,
}
},
}
</script>
當(dāng)使用 modal = false 時(shí),請(qǐng)一定保證 Dialog 的 append-to-body 屬性為 true, 因?yàn)?nbsp;Dialog 的定位是通過(guò) position: relative 來(lái)定位的,當(dāng) modal 不存在當(dāng)時(shí)候,如果不將 Dialog 插入到 Document.Body 下,則會(huì)根據(jù)當(dāng)前的位置進(jìn)行定位,會(huì)導(dǎo)致樣式錯(cuò)亂
參數(shù) | 說(shuō)明 | 類(lèi)型 | 可選值 | 默認(rèn)值 |
---|---|---|---|---|
model-value / v-model | 是否顯示 Dialog | boolean | — | — |
title | Dialog 的標(biāo)題,也可通過(guò)具名 slot (見(jiàn)下表)傳入 | string | — | — |
width | Dialog 的寬度 | string / number | — | 50% |
fullscreen | 是否為全屏 Dialog | boolean | — | false |
top | Dialog CSS 中的 margin-top 值 | string | — | 15vh |
modal | 是否需要遮罩層 | boolean | — | true |
append-to-body | Dialog 自身是否插入至 body 元素上。嵌套的 Dialog 必須指定該屬性并賦值為 true | boolean | — | false |
lock-scroll | 是否在 Dialog 出現(xiàn)時(shí)將 body 滾動(dòng)鎖定 | boolean | — | true |
custom-class | Dialog 的自定義類(lèi)名 | string | — | — |
open-delay | Dialog 打開(kāi)的延時(shí)時(shí)間,單位毫秒 | number | — | 0 |
close-delay | Dialog 關(guān)閉的延時(shí)時(shí)間,單位毫秒 | number | — | 0 |
close-on-click-modal | 是否可以通過(guò)點(diǎn)擊 modal 關(guān)閉 Dialog | boolean | — | true |
close-on-press-escape | 是否可以通過(guò)按下 ESC 關(guān)閉 Dialog | boolean | — | true |
show-close | 是否顯示關(guān)閉按鈕 | boolean | — | true |
before-close | 關(guān)閉前的回調(diào),會(huì)暫停 Dialog 的關(guān)閉 | function(done),done 用于關(guān)閉 Dialog | — | — |
center | 是否對(duì)頭部和底部采用居中布局 | boolean | — | false |
destroy-on-close | 關(guān)閉時(shí)銷(xiāo)毀 Dialog 中的元素 | boolean | — | false |
name | 說(shuō)明 |
---|---|
— | Dialog 的內(nèi)容 |
title | Dialog 標(biāo)題區(qū)的內(nèi)容 |
footer | Dialog 按鈕操作區(qū)的內(nèi)容 |
事件名稱 | 說(shuō)明 | 回調(diào)參數(shù) |
---|---|---|
open | Dialog 打開(kāi)的回調(diào) | — |
opened | Dialog 打開(kāi)動(dòng)畫(huà)結(jié)束時(shí)的回調(diào) | — |
close | Dialog 關(guān)閉的回調(diào) | — |
closed | Dialog 關(guān)閉動(dòng)畫(huà)結(jié)束時(shí)的回調(diào) | — |
更多建議: