第12节:完成用户的新增、修改功能的对接

B站影视 2024-12-03 18:16 2

摘要:正常禁用取消保存import{ref,reactive,watch}from'vue'import{useFormData}from'@/utils/form-data.js'im

正常禁用取消保存import { ref, reactive, watch } from 'vue'import { useFormData } from '@/utils/form-data.js'import { saveUser } from '@/api/user/index.js'import { ElMessage } from 'element-plus'//自定义事件const emit = defineEmits(['done'])//是否显示弹窗const show = defineModel('show')//是否是编辑(用户名不允许修改)const isUpdate = defineModel('isUpdate')//编辑时的用户数据const rowData = defineModel('rowData')//弹窗标题const dialogTitle = defineModel('dialogTitle')//表单对象const formRef = ref(null)//表单const { form, resetFields, assignFields } = useFormData({userId: void 0,username: '',password: '',phone: '',sex: void 0,status: void 0});//验证规则const rules = reactive({username: [{required: true,message: '请输入名称',type: 'string',trigger: 'blur'}],phone: [{required: true,message: '请输入手机号码',type: 'string',trigger: 'blur'}],});//性别选项const selectOpt = [{ value: 1, label: '男' },{ value: 2, label: '女' },{ value: 3, label: '保密' }]//保存const save = => {//console.log(form)formRef.value?.validate?.((valid) => {if (!valid) {return false;}//验证通过后提交保存saveUser(form).then((msg) => {ElMessage.success(msg)//关闭弹窗show.value = false//刷新列表页emit('done')}).catch((err) => {return err && ElMessage.error(err.message)})});}//重置取消const close = => {show.value = false;}//监听编辑watch(show, (showValue) => {if(showValue){if(rowData.value) {assignFields(rowData.value)}} else {resetFields;formRef.value?.clearValidate?.;}}); 新增删除保密正常禁用{{ row.createTime }}编辑删除import { reactive, onMounted, ref } from 'vue'import { Delete, Plus } from '@element-plus/icons-vue'import UserSearch from './components/user-search.vue'import { pageUser } from '@/api/user/index.js'import { ElMessage } from 'element-plus'import UserSave from './components/user-save.vue'//是否显示弹窗const show = ref(false)//是否是编辑const isUpdate = ref(false)//编辑时的用户数据const rowData = ref(null)//弹窗标题const dialogTitle = ref('')//表单属性const tableData = reactive({page: 1, //当前默认页total: 0, //模拟,总记录数pageSize: 10, //每页记录数list: , //用户数据列表});//新增文件const add = => {isUpdate.value = falserowData.value = nulldialogTitle.value = '新增用户'//这个要放在最后show.value = true};//编辑文章const edit = (row) => {rowData.value = row ?? nullisUpdate.value = truedialogTitle.value = '编辑用户'//这个要放在最后show.value = true}//Dom挂载后获取数据onMounted( => {getUserList})//重新加载const reload = (form) => {getUserList(form)};//获取分页数据const getUserList = (objData) => {pageUser({ page: tableData.page, pageSize: tableData.pageSize, ...objData}).then((res) => {//console.log(res)tableData.total = res.total;tableData.list = res.list;}).then((e) => {return e && ElMessage.error(e.message)})};//跳转导航const currentChange = (pageNum) => {tableData.page = pageNumgetUserList}.left-6 {margin-left: 6px;}.table-footer {padding-top: 10px;box-sizing: border-box;display: flex;align-items: center;}.pagination {margin: 0 auto;flex-wrap: wrap;justify-content: center;}.el-table__header {background-color: #f0f0f0;} /*** 提交保存*/export const saveUser = async (data) => {const res = await service.post('/api/sys/user', data);if (res.data.code === 200) {return res.data.message}return Promise.reject(new Error(res.data.message))}

来源:爱就对了a

相关推荐