
正文
vue element 新增、编辑类Dialog公用函数
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
调用
<el-button type="primary" class="my-button" size="small" :loading="editLoading" @click="submitDialogInfo('edit')">确 定</el-button> <el-button type="primary" class="my-button" size="small" :loading="addLoading" @click="submitDialogInfo('add')">确 定</el-button>
axios
// api
import * as category from 'api/subjectManage/category';
js
// 弹框内容提交
submitDialogInfo(formName) {
const that = this;
const _form = `${formName}Form`;
const _loading = `${formName}Loading`;
const _dialog = `${formName}Dialog`;
that.$refs[_form].validate(function(valid) {
that[_dialog] = true;
if (valid) {
// data
const data = that[_form];
// name
let match_name = `${formName}Info`;
for (let key in category) {
if (match_name === key) {
// function
category[key](data)
.then(res => {
that[_loading] = false;
if (res.data && (!res.data.code || res.data.code === 200)) {
// 关闭弹框
that[_dialog] = false;
that.$message.success('数据更新成功');
// 刷新数据
that.getData();
} else {
// 在表单中输出错误提示
let err = res.data.errors;
for (let key in err) {
err[key] = err[key][0];
}
that.formError = err;
}
})
.catch(err => {
that[_loading] = false; // button loading
});
}
}
} else {
that[_loading] = false;
that.$message.error('请检查输入');
}
});
}








