当前位置:   article > 正文

自定义element-plus的弹框样式_elementplus dialog样式修改

elementplus dialog样式修改

项目中弹框使用频繁,需要统一样式风格,此组件可以自定义弹框的头部样式和内容

一、文件结构如下:
 

 

 二、自定义myDialog组件

需求:

1.自定义弹框头部背景样式和文字

2.自定义弹框内容

3.基本业务流程框架

components/myDialog/index.vue完整代码:

  1. <template>
  2. <div class="system-user-dialog-container">
  3. <el-dialog center align-center v-model="state.isShow" width="769px" @close="dialogRefresh">
  4. <template #header="{ titleClass }">
  5. <div :class="titleClass">{{ state.title }}</div>
  6. </template>
  7. <el-form ref="formRef" :rules="state.rules" :model="state.formData" size="default" label-width="100px">
  8. <slot name="formPart">
  9. <div>表单内容</div>
  10. </slot>
  11. </el-form>
  12. <template #footer>
  13. <span class="dialog-footer">
  14. <el-button @click="onCancel" size="default">取 消</el-button>
  15. <el-button type="primary" @click="onSubmit(formRef)" size="default">确认</el-button>
  16. </span>
  17. </template>
  18. </el-dialog>
  19. </div>
  20. </template>
  21. <script setup lang="ts" name="myDialog">
  22. import { ElMessage, FormInstance } from 'element-plus';
  23. import { reactive, ref } from 'vue';
  24. // 定义变量内容
  25. const formRef = ref();
  26. // 定义子组件向父组件传值/事件
  27. const emit = defineEmits(['doSend']);
  28. const state: any = reactive({
  29. isShow: false,
  30. formData: [],
  31. title: '',
  32. rules: {},
  33. });
  34. // 取消
  35. const onCancel = () => {
  36. state.isShow = false;
  37. };
  38. // 提交
  39. const onSubmit = async (formEl: FormInstance | undefined) => {
  40. if (!formEl) return;
  41. //表格验证
  42. await formEl.validate(async (valid: any, fields: any) => {
  43. if (valid) {
  44. emit('doSend');
  45. } else {
  46. ElMessage.warning('表格项验证失败,请检查');
  47. console.log('表格项验证失败!', fields);
  48. }
  49. });
  50. onCancel();
  51. };
  52. const dialogRefresh = () => {
  53. // 清除校验规则
  54. formRef.value?.resetFields();
  55. // 重置表单内容
  56. state.formData = [];
  57. };
  58. /*
  59. * formData:表单数据
  60. * title:标题
  61. * rules:表单校验规则
  62. */
  63. const openDialog = (formData: object, title: string, rules: object) => {
  64. state.isShow = true;
  65. state.formData = formData;
  66. state.title = title;
  67. state.rules = rules;
  68. };
  69. // 暴露变量
  70. defineExpose({
  71. openDialog,
  72. });
  73. </script>
  74. <style scoped lang="scss">
  75. :deep .el-dialog__header {
  76. width: 100%;
  77. height: 60px;
  78. background-color: #5e8dff;
  79. }
  80. :deep .el-dialog__header .el-dialog__title {
  81. font-size: 20px;
  82. color: #fff;
  83. }
  84. :deep .el-dialog__header .el-dialog__close {
  85. color: #fff;
  86. }
  87. </style>

三、使用组件

实现方法:

1.通过插槽自定义弹框中的表单内容

2.父子组件传值

3.ref直接获取dom

 dialogDemo/dialog.vue完整代码:

  1. <template>
  2. <div class="system-dic-dialog-container">
  3. <myDialog ref="myDialogRef" @doSend="handleOperate">
  4. <template #formPart>
  5. <div>自定义表单内容</div>
  6. </template>
  7. </myDialog>
  8. </div>
  9. </template>
  10. <script setup lang="ts" name="systemDicDialog">
  11. import myDialog from '/@/components/myDialog/index.vue';
  12. import { reactive, ref } from 'vue';
  13. // 定义子组件向父组件传值/事件
  14. const emit = defineEmits(['refresh']);
  15. // 定义变量内容
  16. const myDialogRef = ref();
  17. const state = reactive({
  18. data: [],
  19. title: '',
  20. isAdd: false,
  21. rules: {
  22. // area: [{ required: true, message: '请选择区域', trigger: 'blur' }],
  23. // alarmValueName: [{ required: true, message: '请输入告警值名称', trigger: 'blur' }],
  24. // alarmValue: [{ required: true, message: '请输入告警值', trigger: 'blur' }],
  25. // alarmValueUnit: [{ required: true, message: '请选择告警值单位', trigger: 'blur' }],
  26. // alarmText: [{ required: true, message: '请输入告警提示语', trigger: 'blur' }],
  27. },
  28. dialog: {
  29. isShowDialog: false,
  30. type: '',
  31. title: '',
  32. submitTxt: '',
  33. },
  34. });
  35. const openDialog = (row: any) => {
  36. console.log(row, 'row');
  37. console.log(state.data, 'data');
  38. if (row) {
  39. state.title = '修改';
  40. state.isAdd = false;
  41. } else {
  42. state.title = '新增';
  43. state.isAdd = true;
  44. }
  45. myDialogRef.value.openDialog(state.data, state.title, state.rules);
  46. };
  47. // 发请求
  48. const handleOperate = () => {
  49. console.log('校验成功');
  50. state.isAdd ? doAdd() : doEdit();
  51. };
  52. // 添加
  53. const doAdd = () => {
  54. console.log('doAdd');
  55. clearFormData();
  56. emit('refresh');
  57. };
  58. // 修改
  59. const doEdit = () => {
  60. console.log('doEdit');
  61. clearFormData();
  62. emit('refresh');
  63. };
  64. // 清空表单信息
  65. const clearFormData = () => {
  66. state.data = [];
  67. };
  68. // 暴露变量
  69. defineExpose({
  70. openDialog,
  71. });
  72. </script>

添加或编辑成功后,刷新页面

  dialogDemo/index.vue完整代码:

  1. <template>
  2. <div class="notice-bar-container layout-pd">
  3. <el-button type="success" @click="handleOpenDialog">点我打开弹框</el-button>
  4. <openDialog ref="openDialogRef" @refresh="handleRefresh"></openDialog>
  5. </div>
  6. </template>
  7. <script setup lang="ts" name="makeDialogDemo">
  8. import openDialog from './dialog.vue';
  9. import { ref } from 'vue';
  10. const openDialogRef = ref();
  11. const handleOpenDialog = () => {
  12. openDialogRef.value.openDialog();
  13. };
  14. // 刷新页面
  15. const handleRefresh = () => {};
  16. </script>
  17. <style lang="scss" scoped></style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/86104
推荐阅读