vue3 、Element Plus新增公共组件或共用组件----Dialog 对话框组件

本文介绍如何在Vue3中使用ElementPlus组件库自定义Dialog对话框组件,包括子组件与父组件的具体实现方式及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此环境在vue3下,用的是Element Plus组件库。

Dialog 对话框组件样式:

在这里插入图片描述

文件对应位置:

在这里插入图片描述

子组件代码:

<template>
    <div>
        <el-dialog :title="title" :model-value="dialogVisible" :append-to-body="true" :close-on-click-modal="clickClose"
            :width="width" :before-close="handleClose">
            <el-tabs type="border-card" style="margin-top:-20px">
                <el-tab-pane :label="subTitle ? subTitle : title">
                    <slot></slot>
                </el-tab-pane>
            </el-tabs>
        </el-dialog>
    </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, SetupContext } from 'vue'
interface Data {
    name: string;
}
export default defineComponent({
    name: '',
    props: {
        title: { //弹框标题,必传
            type: String,
            default: {}
        },
        dialogVisible: { //控制弹框显示隐藏,必传
            type: Boolean,
            default: {}
        },
        clickClose: { //点击弹框外是否已关闭,非必传
            type: Boolean,
            default: {}
        },
        width: { //弹框宽度,非必传
            type: String,
            default: {}
        },
        subTitle: { //tab标题,必传
            type: String,
            default: {}
        },
    },
    components: {

    },
    setup(props, context) {

        const state = reactive({
            name: '',
        })

        const methods = {
            handleClose: () => {
                context.emit('close', false)
            }
        }
        return {
            ...toRefs(state),
            ...methods,
        }
    },
})
</script>

<style scoped lang='scss'>
.el-dialog__header {
    padding: 10px 10px 10px 20px;
}

.el-dialog__body {
    // max-height: 600px;
    overflow: hidden;
    overflow-y: auto;
}
</style>
父组件代码:
<template>
  <div class="about">
    <h1>This is an about page</h1>
    <el-button text @click="open()">
      弹 框
    </el-button>

    <DialogCom :title="'测试'" :sub-title="'详情'" :dialog-visible="visible" :click-close="true" :width="'50%'"
      @close="cancel">
      <span>
        It should be noted that the content will not be aligned in center by
        default
      </span>
      <div class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="cancel()">
          确 定
        </el-button>
      </div>
    </DialogCom>
  </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, SetupContext } from 'vue'
import DialogCom from "@/components/DialogCom.vue"
export default defineComponent({
  // name: '',
  props: {
  },
  components: {
    DialogCom
  },
  setup(props) {

    const state = reactive({
      // name: '',
      visible: false,
    })
    const methods = {
      open: () => {
        state.visible = true
      },
      cancel: () => {
        state.visible = false
      }
    }
    return {
      ...toRefs(state),
      ...methods
    }
  },
})
</script>

<style scoped lang='scss'>
@media (min-width: 1024px) {
  .about {
    min-height: 100vh;
    display: flex;
    align-items: center;
  }
}

.dialog-footer {
  text-align: center;
  margin-top: 20px;
}
</style>
<template> <div class=“category-container”> <!-- 树形结构展示 --> <el-tree :data=“treeData” :props=“defaultProps” node-key=“id” :highlight-current=“true” :expand-on-click-node=“false” > <template #default=“{ node, data }”> <span class=“custom-node”> {{ node.label }} (ID: {{ data.id }}) </span> </template> </el-tree> </div> </template> <script setup> import { ref, onMounted } from ‘vue’ import request from ‘@/utils/request’ // 根据实际路径调整 import { ElTree } from ‘element-plus’ // 树形数据存储 const treeData = ref([]) // 树形组件配置 const defaultProps = ref({ children: ‘children’, label: ‘title’ }) // 加载树形数据 const loadTreeData = async () => { try { const res = await request.get(‘/category/tree’) // 注意接口路径需要与后端一致 if (res.code === ‘200’) { treeData.value = res.data } else { console.error(‘数据加载失败:’, res.msg) } } catch (error) { console.error(‘请求异常:’, error) } } // 组件挂载时加载数据 onMounted(() => { loadTreeData() }) </script> <style scoped> .category-container { padding: 20px; background: #fff; min-height: 500px; } .custom-node { font-size: 14px; } .el-tree { margin-top: 15px; } </style>以下面这种格式美化树状结构展示,要求给出所有代码<template> <div class=“app-container”> <!-- 操作工具栏 --> <div class=“mb-4”> <el-button type=“primary” @click=“showFormDialog()”>新增分类</el-button> </div> <!-- 树形表格 --> <el-table :data="tableData" row-key="id" :tree-props="{children: &#39;children&#39;}" border default-expand-all style="width: 100%" > <el-table-column prop="id" label="ID" width="80" /> <el-table-column prop="title" label="分类名称" /> <el-table-column label="操作" width="180" align="center"> <template #default="{ row }"> <el-button type="primary" size="small" @click="showFormDialog(row.id)" >修改</el-button> <el-button type="danger" size="small" @click="handleDelete(row.id)" >删除</el-button> </template> </el-table-column> </el-table> <!-- 新增/修改对话框 --> <el-dialog v-model="formVisible" :title="currentId ? &#39;修改分类&#39; : &#39;新增分类" width="30%" > <el-form ref="formRef" :model="form" :rules="rules" label-width="80px" > <el-form-item label="分类名称" prop="title"> <el-input v-model="form.title" placeholder="请输入分类名称" /> </el-form-item> </el-form> <template #footer> <el-button @click="formVisible = false">取消</el-button> <el-button type="primary" @click="submitForm">确认</el-button> </template> </el-dialog> </div> </template> <style scoped> .app-container { padding: 20px; } .mb-4 { margin-bottom: 16px; } </style>
03-22
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值