index.vue
<script setup>
import { onMounted, ref } from "vue";
import Edit from "./components/Edit.vue";
import axios from "axios";// TODO: 列表渲染
const list = ref([]);
const getList = async () => {const res = await axios.get("/list");list.value = res.data;
};
onMounted(() => {getList();
});// TODO: 删除功能
const onDelete = async (id) => {console.log("id", id);await axios.delete(`/del/${id}`);getList();
};// TODO: 编辑功能
const editRef = ref(null);
const onEdit = (row) => {editRef.value.open(row);
};
</script><template><div class="app"><el-table :data="list"><el-table-column label="ID" prop="id"></el-table-column><el-table-column label="姓名" prop="name" width="150"></el-table-column><el-table-column label="籍贯" prop="place"></el-table-column><el-table-column label="操作" width="150"><template #default="{ row }"><el-button type="primary" @click="onEdit(row)" link> 编辑 </el-button><el-button type="danger" @click="onDelete(row.id)" link>删除</el-button></template></el-table-column></el-table></div><Edit ref="editRef" @on-Update="getList"/>
</template><style scoped>
.app {width: 980px;margin: 100px auto 0;
}
</style>
edit.vue
<script setup>
// TODO: 编辑
const emit = defineEmits(["on-Update"]);
import { ref } from "vue";
import axios from "axios";
// 弹框开关
const dialogVisible = ref(false);
const from = ref({id: "",name: "",place: "",
});
const open = (row) => {console.log("row", row);from.value.id = row.id;from.value.name = row.name;from.value.place = row.place;dialogVisible.value = true;
};
const onUpdate = async () => {await axios.patch(`/edit/${from.value.id}`, {name: from.value.name,place: from.value.place,});dialogVisible.value = false;emit("on-Update");
};
defineExpose({open,
});
</script><template><el-dialog v-model="dialogVisible" title="编辑" width="400px"><el-form label-width="50px"><el-form-item label="姓名"><el-input v-model="from.name" placeholder="请输入姓名" /></el-form-item><el-form-item label="籍贯"><el-input v-model="from.place" placeholder="请输入籍贯" /></el-form-item></el-form><template #footer><span class="dialog-footer"><el-button @click="dialogVisible = false">取消</el-button><el-button type="primary" @click="onUpdate">确认</el-button></span></template></el-dialog>
</template><style scoped>
.el-input {width: 290px;
}
</style>