彈出模態(tài)框,常用于消息提示、消息確認(rèn),或在當(dāng)前頁面內(nèi)完成特定的交互操作。
彈出框組件支持函數(shù)調(diào)用和組件調(diào)用兩種方式。
Dialog 是一個函數(shù),調(diào)用后會直接在頁面中彈出相應(yīng)的模態(tài)框。
import { Dialog } from 'vant';
Dialog({ message: '提示' });
通過組件調(diào)用 Dialog 時,可以通過下面的方式進(jìn)行注冊:
import Vue from 'vue';
import { Dialog } from 'vant';
// 全局注冊
Vue.use(Dialog);
// 局部注冊
export default {
components: {
[Dialog.Component.name]: Dialog.Component,
},
};
用于提示一些消息,只包含一個確認(rèn)按鈕。
Dialog.alert({
title: '標(biāo)題',
message: '彈窗內(nèi)容',
}).then(() => {
// on close
});
Dialog.alert({
message: '彈窗內(nèi)容',
}).then(() => {
// on close
});
用于確認(rèn)消息,包含取消和確認(rèn)按鈕。
Dialog.confirm({
title: '標(biāo)題',
message: '彈窗內(nèi)容',
})
.then(() => {
// on confirm
})
.catch(() => {
// on cancel
});
將 theme 選項(xiàng)設(shè)置為 round-button
可以展示圓角按鈕風(fēng)格的彈窗,該選項(xiàng)從 2.10.0 版本開始支持。
Dialog.alert({
title: '標(biāo)題',
message: '彈窗內(nèi)容',
theme: 'round-button',
}).then(() => {
// on close
});
Dialog.alert({
message: '彈窗內(nèi)容',
theme: 'round-button',
}).then(() => {
// on close
});
通過 beforeClose
屬性可以傳入一個回調(diào)函數(shù),在彈窗關(guān)閉前進(jìn)行特定操作。
function beforeClose(action, done) {
if (action === 'confirm') {
setTimeout(done, 1000);
} else {
done();
}
}
Dialog.confirm({
title: '標(biāo)題',
message: '彈窗內(nèi)容',
beforeClose,
});
引入 Dialog 組件后,會自動在 Vue 的 prototype 上掛載 $dialog
方法,在所有組件內(nèi)部都可以直接調(diào)用此方法。
export default {
mounted() {
this.$dialog.alert({
message: '彈窗內(nèi)容',
});
},
};
如果需要在彈窗內(nèi)嵌入組件或其他自定義內(nèi)容,可以使用組件調(diào)用的方式。
<van-dialog v-model="show" title="標(biāo)題" show-cancel-button>
<img src="https://img01.yzcdn.cn/vant/apple-3.jpg" rel="external nofollow" />
</van-dialog>
export default {
data() {
return {
show: false,
};
},
};
方法名 | 說明 | 參數(shù) | 返回值 |
---|---|---|---|
Dialog | 展示彈窗 | options
|
Promise
|
Dialog.alert | 展示消息提示彈窗 | options
|
Promise
|
Dialog.confirm | 展示消息確認(rèn)彈窗 | options
|
Promise
|
Dialog.setDefaultOptions | 修改默認(rèn)配置,對所有 Dialog 生效 | options
|
void
|
Dialog.resetDefaultOptions | 重置默認(rèn)配置,對所有 Dialog 生效 | - | void
|
Dialog.close | 關(guān)閉彈窗 | - | void
|
通過函數(shù)調(diào)用 Dialog
時,支持傳入以下選項(xiàng):
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|---|---|---|
title | 標(biāo)題 | string | - |
width | 彈窗寬度,默認(rèn)單位為px
|
number | string | 320px
|
message | 文本內(nèi)容,支持通過\n 換行 |
string | - |
messageAlign | 內(nèi)容對齊方式,可選值為left right
|
string | center
|
theme | 樣式風(fēng)格,可選值為round
|
string | default
|
className | 自定義類名 | any | - |
showConfirmButton | 是否展示確認(rèn)按鈕 | boolean | true
|
showCancelButton | 是否展示取消按鈕 | boolean | false
|
confirmButtonText | 確認(rèn)按鈕文案 | string | 確認(rèn)
|
confirmButtonColor | 確認(rèn)按鈕顏色 | string | #ee0a24
|
cancelButtonText | 取消按鈕文案 | string | 取消
|
cancelButtonColor | 取消按鈕顏色 | string | black
|
overlay | 是否展示遮罩層 | boolean | true
|
overlayClass | 自定義遮罩層類名 | string | - |
overlayStyle | 自定義遮罩層樣式 | object | - |
closeOnPopstate | 是否在頁面回退時自動關(guān)閉 | boolean | true
|
closeOnClickOverlay | 是否在點(diǎn)擊遮罩層后關(guān)閉彈窗 | boolean | false
|
lockScroll | 是否鎖定背景滾動 | boolean | true
|
allowHtml v2.8.7
|
是否允許 message 內(nèi)容中渲染 HTML | boolean | true
|
beforeClose | 關(guān)閉前的回調(diào)函數(shù),
調(diào)用 done() 后關(guān)閉彈窗, 調(diào)用 done(false) 阻止彈窗關(guān)閉 |
(action, done) => void | - |
transition | 動畫類名,等價(jià)于 transition 的name 屬性 |
string | - |
getContainer | 指定掛載的節(jié)點(diǎn),用法示例 | string | () => Element | body
|
通過組件調(diào)用 Dialog
時,支持以下 Props:
參數(shù) | 說明 | 類型 | 默認(rèn)值 |
---|---|---|---|
v-model | 是否顯示彈窗 | boolean | - |
title | 標(biāo)題 | string | - |
width | 彈窗寬度,默認(rèn)單位為 px
|
number | string | 320px
|
message | 文本內(nèi)容,支持通過 \n 換行 |
string | - |
message-align | 內(nèi)容對齊方式,可選值為 left right
|
string | center
|
theme | 樣式風(fēng)格,可選值為 round-button
|
string | default
|
show-confirm-button | 是否展示確認(rèn)按鈕 | boolean | true
|
show-cancel-button | 是否展示取消按鈕 | boolean | false
|
confirm-button-text | 確認(rèn)按鈕文案 | string | 確認(rèn)
|
confirm-button-color | 確認(rèn)按鈕顏色 | string | #ee0a24
|
cancel-button-text | 取消按鈕文案 | string | 取消
|
cancel-button-color | 取消按鈕顏色 | string | black
|
overlay | 是否展示遮罩層 | boolean | true
|
overlay-class | 自定義遮罩層類名 | string | - |
overlay-style | 自定義遮罩層樣式 | object | - |
close-on-popstate | 是否在頁面回退時自動關(guān)閉 | boolean | true
|
close-on-click-overlay | 是否在點(diǎn)擊遮罩層后關(guān)閉彈窗 | boolean | false
|
lazy-render | 是否在顯示彈層時才渲染節(jié)點(diǎn) | boolean | true
|
lock-scroll | 是否鎖定背景滾動 | boolean | true
|
allow-html v2.8.7
|
是否允許 message 內(nèi)容中渲染 HTML | boolean | true
|
before-close | 關(guān)閉前的回調(diào)函數(shù),
調(diào)用 done() 后關(guān)閉彈窗, 調(diào)用 done(false) 阻止彈窗關(guān)閉 |
(action, done) => void | - |
transition | 動畫類名,等價(jià)于 transition 的 name 屬性 |
string | - |
get-container | 指定掛載的節(jié)點(diǎn),用法示例 | string | () => Element | - |
通過組件調(diào)用 Dialog
時,支持以下事件:
事件 | 說明 | 回調(diào)參數(shù) |
---|---|---|
confirm | 點(diǎn)擊確認(rèn)按鈕時觸發(fā) | - |
cancel | 點(diǎn)擊取消按鈕時觸發(fā) | - |
open | 打開彈窗時觸發(fā) | - |
close | 關(guān)閉彈窗時觸發(fā) | - |
opened | 打開彈窗且動畫結(jié)束后觸發(fā) | - |
closed | 關(guān)閉彈窗且動畫結(jié)束后觸發(fā) | - |
通過組件調(diào)用 Dialog
時,支持以下插槽:
名稱 | 說明 |
---|---|
default | 自定義內(nèi)容 |
title | 自定義標(biāo)題 |
組件提供了下列 Less 變量,可用于自定義樣式,使用方法請參考主題定制。
名稱 | 默認(rèn)值 | 描述 |
---|---|---|
@dialog-width | 320px
|
- |
@dialog-small-screen-width | 90%
|
- |
@dialog-font-size | @font-size-lg
|
- |
@dialog-transition | @animation-duration-base
|
- |
@dialog-border-radius | 16px
|
- |
@dialog-background-color | @white
|
- |
@dialog-header-font-weight | @font-weight-bold
|
- |
@dialog-header-line-height | 24px
|
- |
@dialog-header-padding-top | 26px
|
- |
@dialog-header-isolated-padding | @padding-lg 0
|
- |
@dialog-message-padding | @padding-lg
|
- |
@dialog-message-font-size | @font-size-md
|
- |
@dialog-message-line-height | @line-height-md
|
- |
@dialog-message-max-height | 60vh
|
- |
@dialog-has-title-message-text-color | @gray-7
|
- |
@dialog-has-title-message-padding-top | @padding-xs
|
- |
@dialog-button-height | 48px
|
- |
@dialog-round-button-height | 36px
|
- |
@dialog-confirm-button-text-color | @red
|
- |
將 closeOnPopstate
屬性設(shè)置為 false 即可。
Dialog.alert({
title: '標(biāo)題',
message: '彈窗內(nèi)容',
closeOnPopstate: false,
}).then(() => {
// on close
});
更多建議: