feat: element plus中使用messageBox技巧

flex-api
LCJ-MinYa 1 month ago
parent af6d2292a9
commit 6d4869d90f

@ -166,6 +166,10 @@ const titleArr = [
key: 'scrollSnapType',
title: '滚动吸附效果',
},
{
key: 'elementPlusMessageBoxUse',
title: 'element plus中使用messageBox技巧',
},
];
// @/views/demo/**/*.vue

@ -0,0 +1,181 @@
<template>
<div class="p-5 space-y-5 !bg-gray-100">
<el-card header="element plus中使用messageBox技巧">
<el-button
type="primary"
class="mb-5"
@click="showMessageBox"
>
普通弹出确认框
</el-button>
<!-- 这里添加切换开关switch -->
<div>
<div>
<span>切换新功能开关:</span>
<el-switch
v-model="newSwitch"
class="mr-5"
inline-prompt
active-text="开"
inactive-text="关"
active-value="ON"
inactive-value="OFF"
/>
</div>
<div>
<span>新功能是否弹窗开关:</span>
<el-switch
v-model="newDialogSwitch"
class="mr-5"
inline-prompt
active-text="开"
inactive-text="关"
active-value="ON"
inactive-value="OFF"
/>
</div>
<el-button
type="primary"
@click="doConfirm"
>
弹出确认框同步处理
</el-button>
</div>
</el-card>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
const showMessageBox = () => {
ElMessageBox.confirm('确定要删除吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
ElMessage({
type: 'success',
message: '删除成功',
});
})
.catch(() => {
ElMessage({
type: 'info',
message: '取消删除',
});
});
};
//
const mockRequest = (message, status = true) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
ElMessage.success(message);
resolve(status);
}, 500);
});
};
/**
* doConfirm默认做了一些逻辑处理然后发送了一个请求
* 现在新的需求如下
* 1. 我有一个开关开关打开的时候执行以下操作
* 2. 发起一个接口请求我是新请求1返回true显示一个确认弹窗
* 3. 点击确认发起一个接口请求我是新请求2
* 4. 点击取消发起一个接口请求我是新请求3
* 5. 最后再执行原始请求
* 上面的操作必须是同步的
*/
const newSwitch = ref('ON');
const newDialogSwitch = ref('ON');
// ElMessageBoxloading
const hanldeNewFlow = async () => {
return new Promise(async (resolve) => {
if (newSwitch.value !== 'ON') {
resolve(false);
return;
}
let res = await mockRequest('我是新请求1', newDialogSwitch.value === 'ON');
if (res) {
try {
await ElMessageBox.confirm('确定要删除吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
closeOnClickModal: false,
});
await mockRequest('我是新请求2');
} catch (error) {
await mockRequest('我是新请求3');
}
}
resolve(true);
});
};
// ElMessageBoxloading
const hanldeNewFlowWithLoading = async () => {
return new Promise(async (resolve) => {
if (newSwitch.value !== 'ON') {
resolve(false);
return;
}
let res = await mockRequest('我是新请求1', newDialogSwitch.value === 'ON');
if (res) {
try {
await ElMessageBox.confirm('确定要删除吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
closeOnClickModal: false,
beforeClose: async (action, instance, done) => {
try {
if (action === 'confirm') {
instance.confirmButtonLoading = true;
await mockRequest('我是新请求2');
} else {
instance.cancelButtonLoading = true;
await mockRequest('我是新请求3');
}
} finally {
instance.confirmButtonLoading = false;
instance.cancelButtonLoading = false;
done();
}
},
});
} catch {
// ElMessageBox.confirmcancel
console.log('点击取消');
}
}
resolve(true);
});
};
const doConfirm = async () => {
//
let params = {
id: 1,
};
if (!params.id) {
ElMessage.error('id不存在');
return;
}
/**
* 注意这里这里是新代码执行逻辑
*/
await hanldeNewFlowWithLoading();
mockRequest('我是原始请求');
};
</script>
<style lang="scss" scoped></style>
Loading…
Cancel
Save