优化这段代码:let dataList = async (value) =>{ const res:any = await queryById({ id: value.id }); if(res.status) { parameter.recordData.header = value; parameter.recordData.header.occurrenceTime = dayjs(parameter.recordData.header.occurrenceTime).format('YYYY-MM-DD HH:mm'); let defectSource = defectSourceArr.find(item =>item.id == value.defectSourceId); parameter.recordData.header.stitchingDefectSource = `${defectSource.name}/${value.defectSourceName}`; parameter.recordData.detail = res.data.problemDefectList.map((item)=>{ item.displayScreen = item.displayScreen?item.displayScreen.split(','):[]; item.defectDetailList.defectDetailName = item.defectDetailList.map((item)=>{ return item.defectTypeName+'/'+item.defectDetailName; }); item.defectDetailList.defectDetailName = item.defectDetailList.defectDetailName.join(','); return item; }); getSteps(value.id); }else { parameter.recordData.header = {}; parameter.recordData.detail = []; message.warning('此单据详细数据查询未成功,请检查单据是否存在'); } };
时间: 2024-02-24 09:56:15 浏览: 78
html5.zip_html5_html5帮助文档_site:www.pudn.com
下面是对代码的优化:
```javascript
let dataList = async (value) => {
try {
const res = await queryById({ id: value.id });
if (res.status) {
parameter.recordData.header = {
...value,
occurrenceTime: dayjs(value.occurrenceTime).format('YYYY-MM-DD HH:mm'),
stitchingDefectSource: `${defectSourceArr.find(item => item.id == value.defectSourceId)?.name}/${value.defectSourceName}`
};
parameter.recordData.detail = res.data.problemDefectList.map((item) => {
item.displayScreen = item.displayScreen ? item.displayScreen.split(',') : [];
item.defectDetailList.defectDetailName = item.defectDetailList.map((detail) => {
return `${detail.defectTypeName}/${detail.defectDetailName}`;
}).join(',');
return item;
});
getSteps(value.id);
} else {
parameter.recordData.header = {};
parameter.recordData.detail = [];
message.warning('此单据详细数据查询未成功,请检查单据是否存在');
}
} catch (error) {
console.error(`queryById error: ${error}`);
message.error('获取数据失败,请稍后重试');
}
};
```
优化点如下:
1. 使用 try-catch 语句捕获异常,避免代码抛出错误后程序崩溃。
2. 使用对象解构和展开语法简化代码,避免多次设置对象属性。
3. 使用可选链操作符 `?.` 简化代码,避免因为找不到对象属性而抛出错误。
4. 使用箭头函数和模板字符串简化代码,提高代码可读性和可维护性。
5. 使用 `console.error()` 和 `message.error()` 输出错误信息,便于调试和排查问题。
阅读全文