义务需求:在输入字典名称后,查询验证是否存在这个字典,输入字典名称获取相应的字典Id,一起传个后台;
代码出现的问题:在完成输入字典表单保存时,无法获取字典名称的字典值;
//输入字典名称验证
var checkDictName = (rule, value, callback) => {
if (!value) {
return callback(new Error(“请输入字典名称”));
} else {
this.dictName = value;
this.getDicts()//调用验证是否存在该字典的方法
callback()
}
};
//获取字典,map页面需要的结构,并验证是否存在输入的字典
getDicts() {
let _this = this;
return new Promise(function(resolve, reject){
getDictByName(_this.dictName).then(res => {
if (res.data.data.length > 0) {
//处理获取的数据和下拉框需要的数据对接
let arr = res.data.data;
arr.map(item => {
_this.dictOptions.push({ value: item.id, label: item.name });
});
console.log(_this.dictOptions);
} else {
_this.$message({
type: “warning”,
message: “没有此关联字典”
});
_this.form.dictName = ‘’;
}
resolve();
});
});
},
//保存时获取不到字典名称字段值,利用async await
async submit() {
if (this.formStatus == “add”) {
//通过字典Id获取字段名
await this.getDicts();
//利用输入字典名称获取相应的字典Id,
let dictid = this.selectByKey(
“label”,
this.form.dictName,//不用await时,字典名一直为undefined
this.dictOptions
).value;
console.log(dictid)