Vue3 + Element-plus,el-dialog 最简易好用的封装方法

现在 vue3 官网更新了新方法 defineModel() ,这就是最简单方便的方法了

<template>
  <el-dialog
    title="title"
    v-model="visible"
    width="480px"
    :close-on-click-modal="false"
  >
    <span></span>
    <template #footer>
      <span>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
const visible = defineModel()

</script>

<style lang="scss" scoped>
</style>


———————— 下面是以前的方法——————————

编辑弹窗:

<template>
    <el-dialog v-model="visible" title="Tips" width="30%" >
        <span>This is a message</span>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="visible = false">Cancel</el-button>
                <el-button type="primary" @click="visible = false">
                    Confirm
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
const props = defineProps({
    visible: Boolean,
})
const emits = defineEmits(['update:visible'])

const visible = computed({
  get: () => {
    return props.visible
  },
  set: val => emits('update:visible', val),
})
</script>

<style lang="scss" scoped></style>

父组件:

<el-button type="primary" @click="openDialog"></el-button>
<edit-dialog v-model:visible="dialogVisible"></edit-dialog>
const openDialog = () =>{
  dialogVisible.value = true
}
### 创建基于 Vue 3Element Plus 的 CRUD 组件 创建一个功能齐全的 CRUD (Create, Read, Update, Delete) 组件涉及多个方面,包括数据获取、表单处理以及状态管理。下面是一个详细的指南来构建这样的组件。 #### 使用组合 API 构建响应式逻辑 Vue 3 推出了 Composition API 来替代 Options API,在编写复杂业务逻辑时提供了更好的灵活性和可读性[^1]: ```javascript import { ref } from &#39;vue&#39; // 定义初始数据结构 const items = ref([]) async function fetchItems() { const response = await fetch(&#39;/api/items&#39;) items.value = await response.json() } fetchItems() function addItem(item) { // 假设有一个 POST 请求用于新增项目 } function updateItem(id, updates) { // 更新特定 ID 对应的数据项 } function deleteItem(id) { // 删除指定 ID 的记录 } ``` #### 表格展示与分页支持 Element Plus 提供了丰富的 UI 组件库,其中 `el-table` 是用来显示表格化数据的理想选择。为了实现高效的页面加载体验,通常还需要加入分页控件: ```html <template> <div class="crud-container"> <!-- 数据列表 --> <el-table :data="items" style="width: 100%"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column fixed="right" width="200px"> <template slot-scope="scope"> <el-button @click="editRow(scope.row)">Edit</el-button> <el-popconfirm title="Are you sure?" @onConfirm="deleteRow(scope.$index)"> <el-button type="danger">Delete</el-button> </el-popconfirm> </template> </el-table-column> </el-table> <!-- 分页器 --> <el-pagination layout="prev, pager, next" :total="totalCount"></el-pagination> <!-- 新增/编辑对话框 --> <el-dialog v-model="dialogVisible" title="Form Title"> <el-form :model="formModel"> <el-form-item label="Name"> <el-input v-model="formModel.name"></el-input> </el-form-item> </el-form> <span slot="footer" class="dialog-footer"> <el-button @click="closeDialog()">Cancel</el-button> <el-button type="primary" @click="submitForm()">Submit</el-button> </span> </el-dialog> </div> </template> ``` #### 将操作封装成独立函数并优化用户体验 对于每一个基本的操作(增加、修改、删除),都应该定义相应的事件处理器,并考虑如何提升用户的交互感受。比如当执行成功后给出提示信息;失败情况下则捕获错误并向用户反馈具体原因。 通过上述方法可以有效地利用 Vue 3 及其生态系统中的工具和技术来开发高质量且易于维护的应用程序界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值