vue.js基于ElementUI如何封装CRUD的弹框组件

发布时间:2022-07-04 13:45:15 作者:iii
来源:亿速云 阅读:440

Vue.js基于ElementUI如何封装CRUD的弹框组件

在现代的前端开发中,CRUD(创建、读取、更新、删除)操作是非常常见的需求。为了提升开发效率和代码复用性,我们可以将CRUD操作封装成一个通用的弹框组件。本文将详细介绍如何基于Vue.js和ElementUI封装一个CRUD弹框组件。

1. 项目初始化

首先,确保你已经安装了Vue.js和ElementUI。如果还没有安装,可以通过以下命令进行安装:

npm install vue
npm install element-ui

main.js中引入ElementUI:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

2. 创建CRUD弹框组件

接下来,我们创建一个名为CrudDialog.vue的组件文件。这个组件将包含一个弹框,用于处理CRUD操作。

2.1 组件结构

<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="50%"
    @close="handleClose"
  >
    <el-form :model="form" ref="form" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="form.description"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'CrudDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => ({})
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      form: {
        name: '',
        description: ''
      }
    };
  },
  computed: {
    dialogTitle() {
      return this.isEdit ? '编辑' : '新增';
    }
  },
  watch: {
    visible(newVal) {
      this.dialogVisible = newVal;
    },
    formData: {
      handler(newVal) {
        this.form = { ...newVal };
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:visible', false);
      this.$refs.form.resetFields();
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('submit', this.form);
          this.handleClose();
        }
      });
    }
  }
};
</script>

<style scoped>
.el-form-item {
  margin-bottom: 20px;
}
</style>

2.2 组件说明

2.3 组件逻辑

3. 使用CRUD弹框组件

在父组件中使用CrudDialog组件,示例如下:

<template>
  <div>
    <el-button type="primary" @click="handleAdd">新增</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <CrudDialog
      :visible.sync="dialogVisible"
      :formData="formData"
      :isEdit="isEdit"
      @submit="handleSubmit"
    />
  </div>
</template>

<script>
import CrudDialog from './CrudDialog.vue';

export default {
  components: {
    CrudDialog
  },
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      formData: {},
      isEdit: false
    };
  },
  methods: {
    handleAdd() {
      this.formData = {};
      this.isEdit = false;
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.formData = { ...row };
      this.isEdit = true;
      this.dialogVisible = true;
    },
    handleDelete(row) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableData = this.tableData.filter(item => item !== row);
        this.$message.success('删除成功');
      });
    },
    handleSubmit(form) {
      if (this.isEdit) {
        const index = this.tableData.findIndex(item => item === this.formData);
        this.tableData.splice(index, 1, form);
      } else {
        this.tableData.push(form);
      }
      this.dialogVisible = false;
    }
  }
};
</script>

3.1 父组件说明

3.2 父组件逻辑

4. 总结

通过封装CRUD弹框组件,我们可以大大提升代码的复用性和开发效率。在实际项目中,可以根据需求进一步扩展和优化这个组件,例如添加表单验证、支持更多类型的输入控件等。希望本文能帮助你更好地理解和使用Vue.js和ElementUI进行前端开发。

推荐阅读:
  1. js选择弹框
  2. layer弹框

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue.js elementui crud

上一篇:C++中CLI模块的基本类型有哪些

下一篇:Spring Boot之Validation自定义实现的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》