官方文檔?http://element-cn.eleme.io/#/zh-CN/component/message-box
基礎(chǔ)用法
消息提示彈框
```
點擊打開 Message Boxexportdefault{? ? methods: {? ? ? open() {this.$alert('這是一段內(nèi)容','標題名稱', {? ? ? ? ? confirmButtonText:'確定',? ? ? ? ? callback: action => {this.$message({? ? ? ? ? ? ? type:'info',? ? ? ? ? ? ? message: `action: ${ action }`? ? ? ? ? ? });? ? ? ? ? }? ? ? ? });? ? ? }? ? }? }
點擊打開 Message Boxexportdefault{ methods: { open() {this.$alert('這是一段內(nèi)容','標題名稱', { confirmButtonText:'確定', callback: action => {this.$message({ type:'info', message: `action: ${ action }` }); } }); } } } 點擊打開 Message Boxexportdefault{ methods: { open() {this.$alert('這是一段內(nèi)容','標題名稱', { confirmButtonText:'確定', callback: action => {this.$message({ type:'info', message: `action: ${ action }` }); } }); } } }<template>
? <el-button type="text" @click="open">點擊打開 Message Box</el-button>
</template>
<script>
? export default {
? ? methods: {
? ? ? open() {
? ? ? ? this.$alert('這是一段內(nèi)容', '標題名稱', {
? ? ? ? ? confirmButtonText: '確定',
? ? ? ? ? callback: action => {
? ? ? ? ? ? this.$message({
? ? ? ? ? ? ? type: 'info',
? ? ? ? ? ? ? message: `action: ${ action }`
? ? ? ? ? ? });
? ? ? ? ? }
? ? ? ? });
? ? ? }
? ? }
? }
</script>
```
確認消息彈框
```
? 點擊打開 Message Boxexportdefault{methods: {? ? ? open2() {this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?','提示', {confirmButtonText:'確定',cancelButtonText:'取消',type:'warning'}).then(() => {this.$message({? ? ? ? ? ? type:'success',? ? ? ? ? ? message:'刪除成功!'});? ? ? ? }).catch(() => {this.$message({? ? ? ? ? ? type:'info',? ? ? ? ? ? message:'已取消刪除'});? ? ? ? ? ? ? ? ? });? ? ? }? ? }? }
<template>
? <el-button type="text" @click="open2">點擊打開 Message Box</el-button>
</template>
<script>
? export default {
? ? methods: {
? ? ? open2() {
? ? ? ? this.$confirm('此操作將永久刪除該文件, 是否繼續(xù)?', '提示', {
? ? ? ? ? confirmButtonText: '確定',
? ? ? ? ? cancelButtonText: '取消',
? ? ? ? ? type: 'warning'
? ? ? ? }).then(() => {
? ? ? ? ? this.$message({
? ? ? ? ? ? type: 'success',
? ? ? ? ? ? message: '刪除成功!'
? ? ? ? ? });
? ? ? ? }).catch(() => {
? ? ? ? ? this.$message({
? ? ? ? ? ? type: 'info',
? ? ? ? ? ? message: '已取消刪除'
? ? ? ? ? });? ? ? ? ?
? ? ? ? });
? ? ? }
? ? }
? }
</script>
```
提交內(nèi)容彈框
```
? 點擊打開 Message Boxexportdefault{methods: {? ? ? open3() {this.$prompt('請輸入郵箱','提示', {confirmButtonText:'確定',cancelButtonText:'取消',inputPattern:/[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,inputErrorMessage:'郵箱格式不正確'}).then(({ value }) => {this.$message({? ? ? ? ? ? type:'success',? ? ? ? ? ? message:'你的郵箱是: '+ value? ? ? ? ? });? ? ? ? }).catch(() => {this.$message({? ? ? ? ? ? type:'info',? ? ? ? ? ? message:'取消輸入'});? ? ? ? ? ? ? });? ? ? }? ? }? }
<template>
? <el-button type="text" @click="open3">點擊打開 Message Box</el-button>
</template>
<script>
? export default {
? ? methods: {
? ? ? open3() {
? ? ? ? this.$prompt('請輸入郵箱', '提示', {
? ? ? ? ? confirmButtonText: '確定',
? ? ? ? ? cancelButtonText: '取消',
? ? ? ? ? inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
? ? ? ? ? inputErrorMessage: '郵箱格式不正確'
? ? ? ? }).then(({ value }) => {
? ? ? ? ? this.$message({
? ? ? ? ? ? type: 'success',
? ? ? ? ? ? message: '你的郵箱是: ' + value
? ? ? ? ? });
? ? ? ? }).catch(() => {
? ? ? ? ? this.$message({
? ? ? ? ? ? type: 'info',
? ? ? ? ? ? message: '取消輸入'
? ? ? ? ? });? ? ?
? ? ? ? });
? ? ? }
? ? }
? }
</script>
```
自定義彈框
```
? 點擊打開 Message Boxexportdefault{methods: {? ? ? open4() {consth =this.$createElement;this.$msgbox({title:'消息',message: h('p',null, [? ? ? ? ? ? h('span',null,'內(nèi)容可以是 '),? ? ? ? ? ? h('i', {style:'color: teal'},'VNode')? ? ? ? ? ]),showCancelButton:true,confirmButtonText:'確定',cancelButtonText:'取消',beforeClose:(action, instance, done)=>{if(action ==='confirm') {? ? ? ? ? ? ? instance.confirmButtonLoading =true;? ? ? ? ? ? ? instance.confirmButtonText ='執(zhí)行中...';? ? ? ? ? ? ? setTimeout(() => {? ? ? ? ? ? ? ? done();? ? ? ? ? ? ? ? setTimeout(() => {? ? ? ? ? ? ? ? ? instance.confirmButtonLoading =false;? ? ? ? ? ? ? ? },300);? ? ? ? ? ? ? },3000);? ? ? ? ? ? }else{done();? ? ? ? ? ? }? ? ? ? ? }? ? ? ? }).then(action => {this.$message({? ? ? ? ? ? type:'info',? ? ? ? ? ? message:'action: '+ action? ? ? ? ? });? ? ? ? });? ? ? }? ? }? }
<template>
? <el-button type="text" @click="open4">點擊打開 Message Box</el-button>
</template>
<script>
? export default {
? ? methods: {
? ? ? open4() {
? ? ? ? const h = this.$createElement;
? ? ? ? this.$msgbox({
? ? ? ? ? title: '消息',
? ? ? ? ? message: h('p', null, [
? ? ? ? ? ? h('span', null, '內(nèi)容可以是 '),
? ? ? ? ? ? h('i', { style: 'color: teal' }, 'VNode')
? ? ? ? ? ]),
? ? ? ? ? showCancelButton: true,
? ? ? ? ? confirmButtonText: '確定',
? ? ? ? ? cancelButtonText: '取消',
? ? ? ? ? beforeClose: (action, instance, done) => {
? ? ? ? ? ? if (action === 'confirm') {
? ? ? ? ? ? ? instance.confirmButtonLoading = true;
? ? ? ? ? ? ? instance.confirmButtonText = '執(zhí)行中...';
? ? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? done();
? ? ? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? ? instance.confirmButtonLoading = false;
? ? ? ? ? ? ? ? }, 300);
? ? ? ? ? ? ? }, 3000);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? done();
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? }).then(action => {
? ? ? ? ? this.$message({
? ? ? ? ? ? type: 'info',
? ? ? ? ? ? message: 'action: ' + action
? ? ? ? ? });
? ? ? ? });
? ? ? }
? ? }
? }
</script>
```
options:
參數(shù)類型說明可選值默認值
titleMessageBox 標題string——
messageMessageBox 消息正文內(nèi)容string / VNode——
dangerouslyUseHTMLString是否將 message 屬性作為 HTML 片段處理boolean—false
type消息類型,用于顯示圖標stringsuccess / info / warning / error—
customClassMessageBox 的自定義類名string——
callback若不使用 Promise第献,可以使用此參數(shù)指定 MessageBox 關(guān)閉后的回調(diào)function(action, instance)痊硕,action 的值為’confirm’或’cancel’, instance 為 MessageBox 實例押框,可以通過它訪問實例上的屬性和方法——
showCloseMessageBox 是否顯示右上角關(guān)閉按鈕boolean—true
beforeCloseMessageBox 關(guān)閉前的回調(diào),會暫停實例的關(guān)閉function(action, instance, done)盒揉,action 的值為’confirm’或’cancel’兑徘;instance 為 MessageBox 實例挂脑,可以通過它訪問實例上的屬性和方法;done 用于關(guān)閉 MessageBox 實例——
lockScroll是否在 MessageBox 出現(xiàn)時將 body 滾動鎖定boolean—true
showCancelButton是否顯示取消按鈕boolean—false(以 confirm 和 prompt 方式調(diào)用時為 true)
showConfirmButton是否顯示確定按鈕boolean—true
cancelButtonText取消按鈕的文本內(nèi)容string—取消
confirmButtonText確定按鈕的文本內(nèi)容string—確定
cancelButtonClass取消按鈕的自定義類名string——
confirmButtonClass確定按鈕的自定義類名string——
closeOnClickModal是否可通過點擊遮罩關(guān)閉 MessageBoxboolean—true(以 alert 方式調(diào)用時為 false)
closeOnPressEscape是否可通過按下 ESC 鍵關(guān)閉 MessageBoxboolean—true(以 alert 方式調(diào)用時為 false)
closeOnHashChange是否在 hashchange 時關(guān)閉 MessageBoxboolean—true
showInput是否顯示輸入框boolean—false(以 prompt 方式調(diào)用時為 true)
inputPlaceholder輸入框的占位符string——
inputType輸入框的類型string—text
inputValue輸入框的初始文本string——
inputPattern輸入框的校驗表達式regexp——
inputValidator輸入框的校驗函數(shù)¢先裕可以返回布爾值或字符串牍戚,若返回一個字符串, 則返回結(jié)果會被賦值給 inputErrorMessagefunction——
inputErrorMessage校驗未通過時的提示文本string—輸入的數(shù)據(jù)不合法!
center是否居中布局boolean—false
roundButton是否使用圓角按鈕boolean—false