1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| <script lang="ts"> import { defineComponent, ref, reactive, toRefs, onMounted } from 'vue'; import { getUsers, createUser} from "@/api/users"; import { ElMessage, ElMessageBox, ElNotification } from "element-plus"; import { Search } from '@element-plus/icons-vue'; import FormInstance from "element-plus";
export default defineComponent({ name: "XX", components: {}, setup() { const emyptForm = { alias_name: "", } const state = reactive({ serverUserForm: JSON.parse(JSON.stringify(emyptForm)), tableData:[] }); const columns = [ { label: "用户", prop: "alias_name", width: "200" }, ];
onMounted(() => { getData(); });
const getData = () => { getUsers({}).then(res => { const { data } = res.data state.tableData = data.results; }).catch((error) => { ElMessage.error('获取数据错误!'); }) } const serverUserFormRef = ref<typeof FormInstance>(); const rules = { alias_name: [{ required: true, message: "请输入用户名", trigger: "blur" }] };
const clearAll = () => { state.serverUserForm = JSON.parse(JSON.stringify(emyptForm)); } const handleAdd = (formEl) => { formEl.validate((valid: boolean) => { if (valid) { const params = state.serverUserForm; createUser(params).then(res => { const { code, data } = res.data if(code == 0) { ElNotification({ title: '操作成功', message: h('i', { style: 'color: teal' }, '用户已新增!'), }); getData(); } else { ElMessage.error('新增用户错误,请重试!'); } }).catch(error => { ElMessage.error('服务器修复中,请稍后重试!'); }); clearAll(); } else { return false; } }) } return { serverUserFormRef, ...toRefs(state), rules, handleAdd, columns, }; } }); </script>
|