通过ElementUi在Vue搭建的项目中实现CRUD
    		       		warning:
    		            这篇文章距离上次修改已过429天,其中的内容可能已经有所变动。
    		        
        		                
                在Vue项目中使用Element UI实现CRUD操作,你可以通过以下步骤:
- 确保Element UI已经安装在你的项目中。
- 在你的Vue组件中引入需要的Element UI组件,如el-button、el-table、el-form等。
- 设计你的CRUD界面布局,使用Element UI组件构建表单、表格和按钮。
- 通过Vue的数据绑定和事件处理,实现与Element UI组件的交互。
以下是一个简单的例子,展示如何使用Element UI在Vue中创建一个简单的CRUD界面:
<template>
  <div>
    <el-button type="primary" @click="handleCreate">新建</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form">
        <el-form-item label="日期">
          <el-input v-model="form.date" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></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>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      tableData: [
        // ...初始数据
      ],
      dialogVisible: false,
      dialogTitle: '',
      form: {
        date: '',
        name: '',
        address: ''
      }
    };
  },
  methods: {
    handleCreate() {
      this.dialogVisible = true;
      this.dialogTitle = '新建';
      this.form = {}; // 清空表单
    },
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.dialogTitle = '编辑';
      this.form = { ...row }; // 复制行数据到表单
    },
    handleDelete(index, row) {
      this.tableData.splice(index, 1); // 删除表格中的数据
    },
    handleSubmit() {
      if (this.dialogTitle === '新建') {
        // 添加数据到表格
        this.tableData.push({ ...this.form }
评论已关闭