const getClassifyData = ()=>{ let pararms = {name: nodeLable.value} getClassify(pararms).then(res=>{ TypeSelects.value = res.result if(parId.value){ return getClassify({classId:parId.value}) } }).then(res=>{ childOptions.value = res.result if(claId.value){ return getClassify({ indexId: claId.value }) } }).then(res=>{ lastResult.value = res.result tableData.value = res.result.map(obj => { const dashIndex = obj.indexFinalName.indexOf("-"); const dataType = obj.indexFinalName.substring(0, dashIndex) + "-基础数据"; return { ...obj, dataType }; }) }).catch(error=>{ console.log('error', error) }) }改进代码
时间: 2024-02-18 09:05:13 浏览: 177
这段代码可以进行一些改进,例如:
1. 使用 async/await 替代 Promise 的 then() 和 catch() 方法,使代码更易读。
2. 将函数参数改为传入 pararms 对象,使代码更灵活。
3. 使用 try/catch 捕获错误,而不是使用 catch() 方法。
改进后的代码如下:
```
const getClassifyData = async (params) => {
try {
const res1 = await getClassify({ name: params.name });
TypeSelects.value = res1.result;
if(params.parId){
const res2 = await getClassify({ classId: params.parId });
childOptions.value = res2.result;
if(params.claId){
const res3 = await getClassify({ indexId: params.claId });
lastResult.value = res3.result;
tableData.value = res3.result.map(obj => {
const dashIndex = obj.indexFinalName.indexOf("-");
const dataType = obj.indexFinalName.substring(0, dashIndex) + "-基础数据";
return { ...obj, dataType };
});
}
}
} catch (error) {
console.log('error', error);
}
}
```
这样就可以更简洁地实现同样的功能,并且代码更易读。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)